Nik*_*zic 1 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
Run Code Online (Sandbox Code Playgroud)
但是当我使用该文件路径在片段中设置图像时,它工作正常。
这是我的代码:
String urilogo = helper.getUri();
File imgFile = new File(urilogo);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
在它起作用的片段中:
String urilogo = helper.getUri();
if(urilogo!= null || urilogo!= "") {
imageView.setImageURI(Uri.parse(urilogo));
}
Run Code Online (Sandbox Code Playgroud)
如果您不关心文件名,可以将InputStream用作:
InputStream is = getContentResolver().openInputStream(urilogo);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
Run Code Online (Sandbox Code Playgroud)
如果上面的代码不起作用并且 helper.getUri()是fileUri,则使用:
String urilogo = helper.getUri();
File imgFile = new File(new URI(urilogo));
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5413 次 |
| 最近记录: |