Selecting a photo from the gallery and viewing in the image view

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(); // String picturePath contains the path of selected Image // Show the Selected Image on ImageView ImageView imageView = (ImageView) findViewById(R.id.imgView); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } 
+6
source share
5 answers

I am facing similar problems like getting uri cursor from resource, open stream, setting bitmap etc. And he constantly has mistakes.

So, I searched the libraries and found the library of the image selector library.

I bet you would like to try this project from image-chooser-library

It is very easy to use and solves all these nitty gritty problems for you, such as picasa images, etc.

Hope this is helpful for you.

+5
source

try it

 public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); switch (requestCode) { case RESULT_LOAD_IMAGE: if (resultCode == Activity.RESULT_OK) { Uri selectedImage = intent.getData(); try { Bitmap bitmapImage =decodeBitmap(selectedImage ); } catch (FileNotFoundException e) { e.printStackTrace(); } // Show the Selected Image on ImageView ImageView imageView = (ImageView) findViewById(R.id.imgView); imageView.setImageBitmap(bitmapImage); } 

AND

 public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); final int REQUIRED_SIZE = 100; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) { break; } width_tmp /= 2; height_tmp /= 2; scale *= 2; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); } 
+5
source

The way you are trying to load a bitmap into onActivityResult() is not entirely correct. Sometimes you won’t be able to open the image, and your application may crash. You better use this code:

 Uri imageUri = data.getData(); InputStream imageStream = null; try { imageStream = getContentResolver().openInputStream(imageUri); ImageView imageView = (ImageView) findViewById(R.id.imgView); imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream)); } catch (FileNotFoundException e) { // Handle the error } finally { if (imageStream != null) { try { imageStream.close(); } catch (IOException e) { // Ignore the exception } } } 
+3
source

Add this after imageView.setImageBitmap (BitmapFactory.decodeFile (picturePath));

 imageView.setImageURI(selectedImage); 

This works for me.

0
source
  final int SELECT_PICTURE = 1; public void openGallary(){ Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"SelectPicture"),SELECT_PICTURE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case SELECT_PICTURE: Uri selectedImageUri = data.getData(); imgPerview.setImageURI(selectedImageUri);}} 
-1
source

All Articles