我想将文件路径转换为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
答案 0 :(得分:0)
尝试使用以下代码
Uri uri = Uri.fromFile(new File(filepath));
答案 1 :(得分:0)
感谢大家的帮助。我找到了解决方案。文件路径很好,但我认为它是一个空文件。我不得不这样写:
private File createImageFile(Bitmap image) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File directory = this.getFilesDir();
// Save Bitmap
this.photo = new File(directory, imageFileName + ".jpg");
this.photo.createNewFile();
// Write to/Compress the Bitmap from the camera intent to the file
FileOutputStream fos = new FileOutputStream(this.photo);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
// path to file
this.imgPath = this.photo.getAbsolutePath();
return this.photo;
}