I have an application in which there is a button for selecting a photo from your gallery, and it works fine, and after selecting the image, my application application returned to activity and displayed the image in the image.
Everyone works fine, but sometimes when I select some images, the preview is not displayed. I also tried to compress the image until it works
My code is below. In onCreate()
galeryBtn=(Button)findViewById(R.id.buttonGallery); galeryBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } });
In onActivityResult (int requestCode, int resultCode, Intent 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();
source share