Unable to decode stream

好久不见. 提交于 2020-01-24 09:14:33

问题


I want to save an image into Bitmap by using the absolute path of the image file, below is my code:

Log.d("PhotoPath", selected.getImagePath());
File file = new File(selected.getImagePath());
if(file.exists())
{
    Log.d("File", "Exist");
    Bitmap d = new BitmapDrawable(getResources(), selected.getImagePath()).getBitmap();
    int nh = (int) (d.getHeight() * (512.0 / d.getWidth()));
    Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
    iv_Photo.setImageBitmap(scaled);
}
else
    Log.d("File", "Not exist");

Below is my output including the exception:

D/PhotoPath: /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg
D/File: Exist
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg (Permission denied)
W/BitmapDrawable: BitmapDrawable cannot decode /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg

What is the problem? I did add WRITE_EXTERNAL_STORAGE permission in Manifest.


回答1:


Please see below here, you need to decode file path then you will get bitmap.

Log.d("PhotoPath", selected.getImagePath());
File file = new File(selected.getImagePath());
if(file.exists())
{
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
else
    Log.d("File", "Not exist");


来源:https://stackoverflow.com/questions/41216176/unable-to-decode-stream

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