无法解码流

时间:2016-12-19 05:23:31

标签: android filenotfoundexception

我想通过使用图像文件的绝对路径将图像保存到位图中,下面是我的代码:

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");

以下是我的输出,包括例外:

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

有什么问题?我确实在Manifest中添加了WRITE_EXTERNAL_STORAGE权限。

1 个答案:

答案 0 :(得分:-1)

请在下面看到,您需要解码文件路径,然后才能获得位图。

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");