Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Image Picker, Wrong Image

I am starting a request for an image pick:

Intent intent = new Intent();
intent.setType( "image/*" );
intent.setAction( Intent.ACTION_GET_CONTENT );
startActivityForResult( Intent.createChooser( intent, "Choose"), PHOTO_GALLERY );

And getting the data back out in onActivityResult:

if( resultCode == Activity.RESULT_OK && requestCode == PHOTO_GALLERY )
{
  U.log( data.getData() );
  Bitmap bm = ... // built from the getData() Uri
  this.postImagePreview.setImageBitmap( bm );
}

When I launch the Intent, I see some folders, such as sdcard, Drop Box, MyCameraApp, and so on.

If I chose a picture from sdcard, when I load the preview, it is the completely wrong image. The other folders don't seem to be giving me this problem.

Does anyone know why it'd let me pick one image, then give me the Uri for another?

EDIT: Here are some exampled logged getData()s:

Good:

content://com.google.android.gallery3d.provider/picasa/item/5668377679792530210

Bad:

content://media/external/images/media/28

EDIT: I'm still having issues, when picking from the sdcard folder of gallery.

Here is a bit more expansion of what I'm doing in onActivityResult:

// cursor
Uri selectedImage = data.getData();

String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

int columnIndex = cursor.getColumnIndex( filePathColumn[0] );
String filePath = cursor.getString( columnIndex );
cursor.close();

// Cursor: /mnt/sdcard/Pic.jpg : /mnt/sdcard/Pic.jpg
U.log( "Cursor: " + filePath + " : " + Uri.parse( filePath ) );

// "regular" 

// Regular: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular: " + data.getDataString() + " : " + Uri.parse( data.getDataString() ) );

// Regular 2: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular 2: " + data.getData() + " : " + data.getData() );


mPostImagePreview.setImageBitmap( BitmapFactory.decodeFile( filePath ) );
mPostImagePreview.setVisibility( View.VISIBLE );

They still set the wrong image. If I go into the Gallery, long press the image, and view its details I get:

TItle: Pic
Time: May 2, 2012
Width: 720
Height: 1280
Orientation: 0
File size: 757KB
Maker: Abso Camera
Model: Inspire 4G
Path: /mnt/sdcard/Pic.jpg

So, the Gallery is telling me the path is the same as the pick action, and the Gallery is rendering it correctly. So why on earth is it not rendering if I set it from onActivityResult?

Also, this is the code I'm using to fire the Intent now:

private void selectPhoto()
{
  Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
  intent.setType( "image/*" );
  ( ( Activity )mContext ).startActivityForResult( Intent.createChooser( intent, "Select Picture" ), PHOTO_GALLERY );
}
like image 392
Josh Avatar asked Apr 23 '12 21:04

Josh


2 Answers

Sometimes the thumbnails in the gallery app can be outdated and show thumbnails for a different image. This can happen when the image ids are reused, for example when an image gets deleted and a new one is added using the same id.

Manage Apps > Gallery > Clear Data can fix this problem then.

like image 151
uwe Avatar answered Oct 03 '22 03:10

uwe


This is the code to open gallery. However this the same what you have done. Also see the onActivityResult code which I used to retrive the selected image.

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),  
                       PHOTO_GALLERY);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
    case PHOTO_GALLERY:
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = Uri.parse(data.getDataString());
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(  
                                   getApplicationContext().getContentResolver(),   
                                   selectedImageUri);

                this.postImagePreview.setImageBitmap( bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}
like image 35
Shankar Agarwal Avatar answered Oct 03 '22 04:10

Shankar Agarwal