Drawable image = Drawable.createFromPath(newImagepath);
defective_image.setBackgroundDrawable(image);
圖像存儲在字符串newImagePath變量中。上面引發了一個outOfMemory異常。無法從SDCard中檢索圖像
我也嘗試將圖像檢索到Bitmap對象中。
Bitmap bitmapImg= BitmapFactory.decodeFile(newImagepath);
在上述情況下,bitmapImg爲空。 (沒有任何例外)
但是,如果它作爲文件檢索,它是成功的。這不符合我的目的,因爲我希望這個圖像成爲RelativeLayout的背景。
File imageFile = new File(newImagepath);
String imgPath = imageFile.getAbsolutePath();
imgPath與newImagePath相同,得出結論路徑沒有錯。我還驗證了SDCard中圖像的存在。
此圖像是從設備的相機拍攝的。此代碼正在仿真器上成功運行。在設備上進行調試時,上述故障已被注意到。 我也試過: -
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
你有'<使用許可權的android:NAME = 「android.permission.READ_EXTERNAL_STORAGE」/>'在你的清單? – Stan
<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> 我有WRITE。這不適用我也可以閱讀嗎?而且我可以將它讀入一個File對象。只有位圖或可繪製對象正在創建一個問題。 –
由於將圖像檢索到Drawable對象中會引發OutOfMemory異常,因此我認爲這是圖像相關問題的大小? –