【发布时间】:2016-11-24 16:13:51
【问题描述】:
我的应用程序允许用户使用外部相机活动拍照作为某些数据收集的一部分,并将生成的文件路径的 Uri.toString() 存储在模型中以供以后使用。
稍后在回收站视图中查看集合时,由于加载的图像的大小,应用程序会变慢,因此我实现了 Google 的解决方案 here,但是我在堆栈跟踪中得到了 FileNotFoundExceptions,并且图像未加载。视图的其余部分加载正常,应用没有崩溃。
如前所述,uriString 是content:foo/bar 格式的字符串。我尝试过的解决方案包括。
- 在模型中存储
file.getabsolutePath()而不是uri.toString。这会将 uriString 格式更改为file:/foo/bar,但仍然不起作用 - 致电
Uri.parse(uriString).getPath() - 从
uriString创建一个新的文件对象,并从中调用getAbsolutePath()和getPath()。
需要明确的是,调用imageView.setImageURI(Uri.parse(this.item.getPhotoURI())); 绝对有效。所以文件存在并且 uriString 可以被 Android 解释。有问题的功能如下。下面是我的堆栈跟踪。
private Bitmap decodeSampledBitmapFromFile(String uriString, int reqWidth, int reqHeight)
{
// First we do this just to check dimensions (apparently)
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uriString, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// And then return with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(uriString, options);
}
还有我的堆栈跟踪:
11-24 16:10:57.762 17199-17199/uk.mrshll.matt.accountabilityscrapbook E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Android/data/uk.mrshll.matt.accountabilityscrapbook/files/Pictures/JPEG_20161124_160913_-1155698030.jpg: open failed: ENOENT (No such file or directory)
【问题讨论】:
-
文件名可能是问题所在。 JPEG_20161124_160913_-1155698030.jpg 注意 11556...0 开头之前的破折号,尽量避免在文件名的任何地方使用破折号。
标签: android path bitmapfactory