Android - Image Picker, Wrong Image
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.
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);@Overrideprotected 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; } }}
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)); } }