8

I have an app, which has a button to select a photo from your gallery and it works fine and after selecting the image my app show came back to the activity and shows the image in an image View.

Every is working fine but sometimes ,when i select some particular images the preview is not showing. I have also tried to compress the image still its not working

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));

} 

4 Answers 4

6

Try like this

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);
    }
1
  • Your Code is work well.But how to increase the with and Height of the ImageView?
    – reegan29
    May 18, 2015 at 11:12
5

I run into similar problems like getting cursor uri from resource, open stream, set bitmap etc. And it has bugs all the time.

So I searched libraries and found image-chooser-library library.

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

It is very easy to use and solves all those nitty gritty problems for you, like images from picasa etc.

Hopefully it is useful for you.

4
  • i have tested your but when i select some images from phone it giving toest "file not found"
    – Biplab
    Jan 15, 2014 at 13:20
  • Did you select images from highlight? Is so, check out dev_get_content branch and see whether it works.
    – Ethan Fang
    Jan 15, 2014 at 13:22
  • Hey also if you think there is a bug, you can create an issue there :) The author will appreciate your effort.
    – Ethan Fang
    Jan 15, 2014 at 13:23
  • thank you for the code, Ur code is working absolutely fine. I have tested it
    – Biplab
    Jan 15, 2014 at 16:58
3

The way you're trying to load a bitmap in onActivityResult() is not absolutely right. Sometimes you will not be able to open an image and your application can crash. You'd better use code like this:

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
        }
    }
}
0

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

imageView.setImageURI(selectedImage);

It work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.