Purplemonkey
Purplemonkey

Reputation: 1967

android load an image from gallery with int ID?

hi I'm trying to load an image into a custom class, this class currently uses an int ID of an internal resource image, but I want the option to use gallery or photo images as well.

this is the current piece of code which creates the view with an internal resource image.

 public TouchExampleView(Context context, AttributeSet attrs, int defStyle, int pic) {
        super(context, attrs, defStyle);
        Log.i(TAG, "pic before: "+pic);
        if (pic ==0) pic = getResources().getIdentifier("ic_launcher" , "drawable", "com.example.testimage");
        Log.i(TAG, "pic afgter: "+pic);
        //mIcon = context.getResources().getDrawable(R.drawable.testpic);
        mIcon = context.getResources().getDrawable(pic);

        mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());

        mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
    }

You can see above the code is using "pic" to allow the internal resource ID to be passed through. however I'd like to use the ID brought from the mediastore as shown here:

final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
                final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
                Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
                if(imageCursor.moveToFirst()){
                    int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
                    TouchExampleView view = new TouchExampleView(this, null, 0, id);
                    }

unfortunately its not working.

Upvotes: 0

Views: 925

Answers (1)

AnujMathur_07
AnujMathur_07

Reputation: 2596

Why don't you just make another constructor with a drawable

Like

 public TouchExampleView(Context context, AttributeSet attrs, int defStyle, Drawable pic) {
    super(context, attrs, defStyle);
    Log.i(TAG, "pic before: "+pic);
    if (pic == null) pic = getResources().getDrawable(R.drawable.ic_launcher);
    Log.i(TAG, "pic afgter: "+pic);
    //mIcon = context.getResources().getDrawable(R.drawable.testpic);
    mIcon = pic;

    mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());

    mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
}

and get drawable like this

Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.TITLE };

    Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();
    File file = new File(filePath);
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    Drawable d = new BitmapDrawable(getResources(),bitmap);

now create a new object of TouchExampleView with the Drawable

new TouchExampleView(context, attrs, defStyle, d)

Hope it helps...

Upvotes: 1

Related Questions