12

I'm trying to load an image from external storage. I set the permissions, I tried different ways, but none of them works.

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap bitmap = BitmapFactory.decodeFile(file.toString()); 

    tv.setImageBitmap(bitmap);

and this one,

FileInputStream streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn); 

    tv.setImageBitmap(bitmap);
        streamIn.close();
tshepang
  • 12,111
  • 21
  • 91
  • 136
Ramin Anushir
  • 175
  • 2
  • 3
  • 8
  • what is tv in the above code. is it a textview? – Raghunandan Apr 10 '13 at 06:32
  • I'm not getting any error, since I put in a try and catch, but the application crashes if I don't have it. ImageView tv = (ImageView) findViewById(R.id.imageView1); – Ramin Anushir Apr 10 '13 at 06:41
  • 1
    **Don't use silent catch blocks during debugging.** Make sure you at least print the stack trace of any Exceptions - otherwise you are left guessing, and end up posting an incomplete question. Voting to close the question on that grounds, and because you apparently resolved your problem a year ago anyway. – Chris Stratton Mar 20 '14 at 15:01

5 Answers5

25

If i have file abc.jpg on the sdcard then:

String photoPath = Environment.getExternalStorageDirectory() + "/abc.jpg";

and to get bitmap.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);

or

Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);

to avoide out of memory error I suggest you use the below code...

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

To avoid above issue you can use Picasso (A powerful image downloading and caching library for Android)

Documentation

How To?

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
Graham Russell
  • 997
  • 13
  • 24
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • I got it working, I guess the way I had it was fine, the problem was that I was trying to run it as a function, and then call a function, it works if I don't use functions. I'm not sure why it doesn't work when I use functions. Where would you define the function and where can you call it? – Ramin Anushir Apr 10 '13 at 07:14
  • if I use this one " options.inSampleSize = 8; " image becoming very small and clarity also decreasing. – Prashanth Debbadwar Jul 07 '15 at 07:13
5
File sdCard = Environment.getExternalStorageDirectory();

File directory = new File (sdCard.getAbsolutePath() + "/Pictures");

File file = new File(directory, "image_name.jpg"); //or any other format supported

FileInputStream streamIn = new FileInputStream(file);

Bitmap bitmap = BitmapFactory.decodeStream(streamIn); //This gets the image

streamIn.close();
Shiv
  • 4,569
  • 4
  • 25
  • 39
0

Get the path of the image from your folder as below. And then decode the file as a bitmap.

   File file= new File(android.os.Environment.getExternalStorageDirectory(),"Your folder");
   Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

If you have a file path, just use BitmapFactory directly, but tell it to use a format that preserves alpha:

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

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
Austin Kregel
  • 715
  • 1
  • 8
  • 27
rishikesh
  • 15
  • 5
0

There is a function called

createFromPath(String)

in the Drawable class. So the statement

String path="/storage/..<just type in the path>";
Drawable.createFromPath(path);

will return a drawable object

Samarth S
  • 313
  • 4
  • 5