-3

I try show a bitmap from the gallery. this is my URL

file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg

I know how to get bitmap using onActivityResult(), but I don't know how to get a bitmap

this is my source

 final ImageView imageView=(ImageView)findViewById(R.id.imageView);

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        final Bitmap bitmap = BitmapFactory.decodeFile("file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg", options);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setImageBitmap(bitmap);
            }
        });

How can I solve my problem?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
BekaKK
  • 2,173
  • 6
  • 42
  • 80

3 Answers3

0

please refer below code

 BitmapFactory.Options options = new BitmapFactory.Options();

            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //Get our saved file into a bitmap object:
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "DCIM"+ File.separator + "Camera "+File.separator + "IMG_20161103_180603.jpg");
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            imageView.setImageBitmap(bitmap);
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

I had the same problem caused by large image size which couldn't be processed at run time. Solved it using this.

// Decodes image and scales it to reduce memory consumption
public static Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 200;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

where File f will be retrieved using https://stackoverflow.com/a/20559418/3758972

just put the size of thumbnail you want (typically the size of your imageview) in REQUIRED_SIZE and you are done. It will give you a scaled image which you can set to your imageview.

Community
  • 1
  • 1
Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36
0
decodeFile( "file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg

Change to:

decodeFile( "/storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg
greenapps
  • 11,154
  • 2
  • 16
  • 19