1

I am creating an Android app in which you copy an image from location x to location y. After the copying is complete I would like to see the picture in a ImageView. I know the images location, but no matter what I try I can't create a bitmap object of it.

The line which are causing my problems is this:

BitmapFactory.decodeFile(dir+s);

dir = getCacheDir().getAbsolutePath()+"/images/";

s = file name (eg. 1275123123.jpg)

If I create a File object with the same path, and call f.isFile(), it returns true.

Opening the image in either android or windows are not a problem.

MartinHaTh
  • 1,417
  • 1
  • 13
  • 25

3 Answers3

7

Didn't you forget to add SD card access permissions READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE ?

zedrummer
  • 71
  • 1
  • 3
2

I had a similar problem and what eventually solved it was creating FileInputStream(...) and decoding the stream. This is the code I used, I changed it to suite your case:

File location = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/images");
File dest = new File(location, fileName + ".JPG");
FileInputStream fis;
fis = new FileInputStream(dest);
Bitmap img = BitmapFactory.decodeStream(fis);
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • I'm getting FileNotFoundException no creating fis. While File object is fine. Please help – Hussain Mansoor Oct 07 '13 at 07:06
  • 1
    No need to do this. `decodeFile` calls `decodeStream` itself: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/graphics/BitmapFactory.java#BitmapFactory.decodeFile%28java.lang.String%29 – Or B Feb 22 '14 at 00:46
-1

You have to add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

permissions to you manifest.

And please don't forget to request runtime permission from user.