【发布时间】:2017-07-10 14:06:47
【问题描述】:
我想将文件路径转换为 uri。但我收到错误说它无法解析 uri。图像来自从相机意图保存图像。图像保存到这个目录:getFilesDir();在应用程序中。
在我的光标适配器中:
String imgPath = cursor.getString(cursor.getColumnIndexOrThrow( InventoryContract.InventoryEntry.COLUMN_PRODUCT_IMG_PATH ));
String productName = cursor.getString(cursor.getColumnIndexOrThrow( InventoryContract.InventoryEntry.COLUMN_PRODUCT_NAME ));
int productStock = cursor.getInt(cursor.getColumnIndexOrThrow( InventoryContract.InventoryEntry.COLUMN_PRODUCT_STOCK ));
Uri uri = ContentUris.withAppendedId(InventoryContract.InventoryEntry.CONTENT_URI,
cursor.getLong(cursor.getColumnIndexOrThrow( InventoryContract.InventoryEntry.COLUMN_PRODUCT_ID )));
File imgFile = new File(imgPath);
Uri imgURI = Uri.fromFile(imgFile);
if(imgFile.exists()) {
Log.v("image file" , String.valueOf(imgFile));
//Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
productImage.setImageURI(imgURI);
Log.v("Image does exist", "file | " + String.valueOf(imgURI));
}
else {
Log.v("bitmap --- ", "Could not find file | " + imgPath);
}
错误行之一:
W/ImageView: resolveUri failed on bad bitmap uri: file:///data/user/0/com.example.android.inventoryapp/files/JPEG_20170710_094130_1552703275.jpg
【问题讨论】:
-
不要使用
setImageURI()。这将在主应用程序线程上加载图像,这将冻结您的 UI 一段时间。使用图像加载库(例如 Glide、Picasso),为库提供您的File对象。 -
我找到了解决办法,谢谢!
-
这是一个来自 Udacity.com 的项目,标准禁止外部库/框架。
-
那么,我不能推荐 Udacity 课程。这是在面试、代码审查等中会困扰你的事情。
-
我同意该标准存在缺点。他们说目的是熟悉原始 Java,但为什么要重新发明轮子。尽管我仍然认为整个课程都很好。它被称为:谷歌纳米学位的Android基础