BitmapFactory:无法解码流:java.io.FileNotFoundException:open failed:ENOENT(没有这样的文件或目录)

时间:2018-02-02 09:13:49

标签: java android bitmap

我试图从文件路径获取有关BitMapFactory.decodeFile的图片,以便在Canvas中绘制位图并获得此异常:

 Unable to decode stream: java.io.FileNotFoundException: 
/content:/media/external/images/media/40: open failed: ENOENT (No such file 
or directory)
02-02 10:03:19.793 3371-3371/com.group.digit.razvoj.appointment 
E/AndroidRuntime: FATAL EXCEPTION: main

但是当我在Fragment中使用该文件路径来设置setImage时,它可以正常工作。

这是我的代码:

 String urilogo = helper.getUri();
        File imgFile = new  File(urilogo);
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

在片段中它起作用:

String urilogo = helper.getUri();        
    if(urilogo!= null || urilogo!= "") {
        imageView.setImageURI(Uri.parse(urilogo));
    }

1 个答案:

答案 0 :(得分:0)

如果您对文件名不感兴趣,可以使用InputStream作为:

InputStream is = getContentResolver().openInputStream(urilogo);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close(); 

如果以上代码无效并且helper.getUri()file Uri则使用:

String urilogo = helper.getUri();
File imgFile = new File(new URI(urilogo));
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());