2013-05-13 242 views
1

我創建中,你從一個地方x複製圖像位置y Android應用程序。複製完成後,我想看到ImageView中的圖片。 我知道圖片的位置,但無論我怎麼努力,我不能創建它的bitmap對象。BitmapFactory.decodeFile()返回null

這是導致我的問題,該生產線是這樣的:

BitmapFactory.decodeFile(dir+s); 

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

s = file name (eg. 1275123123.jpg)

如果我創建一個具有相同路徑的File對象,並調用f.isFile(),它返回true

開口Android或窗口圖像是沒有問題的。

+0

什麼'真正的路徑? – Blackbelt 2013-05-13 21:01:18

+0

你檢查http://stackoverflow.com/questions/6013029/android-decodefile-always-returns-null-for-file-in-internal-storage – Ryan 2013-05-13 21:17:22

+0

@blackbelt /data/data/net.hath.snapcap/cache /images/file.jpg – MartinHaTh 2013-05-13 21:17:28

回答

2

我有一個類似的問題,什麼終於解決它是創造FileInputStream(...)和流進行解碼。這是我使用的代碼,我改變了它適合你的情況:

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

我得到FileNotFoundException沒有創建FIS。雖然File對象很好。請幫忙 – Husyn 2013-10-07 07:06:01

+0

不需要這樣做。 'decodeFile'調用'decodeStream'本身: 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% – 2014-02-22 00:46:45

5

你沒忘了加上SD卡的訪問權限READ_EXTERNAL_STORAGE和/或WRITE_EXTERNAL_STORAGE?