Android - Image Picker, Wrong Image

前提是你 提交于 2019-12-03 23:37:21

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.

Shankar Agarwal

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;
        }
    }
}
Parag Chauhan

private static int RESULT_LOAD_IMAGE = 1;

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);

OnActivity Result

  @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
        }

try this one

 //Put this code on some event 

   Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

 startActivityForResult(intent, REQUEST_CODE);

           // When above event fire then its comes to this 
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
              if (resultCode==RESULT_OK && requestCode==1){

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

                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    filePath = cursor.getString(columnIndex);
                    cursor.close();

                            // Use it as per recruitment 
                    actualBitmap =BitmapFactory.decodeFile(filePath);

              }
    }

Try this,

public class SelectPhotoActivity extends Activity {

private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(intent, SELECT_PICTURE); 
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
           // here you can set the image
            }
    }
}

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!