BitmapFactory.decodeFile() returns null

▼魔方 西西 提交于 2020-06-10 06:46:50

问题


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.


回答1:


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




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



回答3:


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.



来源:https://stackoverflow.com/questions/16531052/bitmapfactory-decodefile-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!