Java file.exists()返回false,但图像uri存在

Java file.exists()返回false,但图像uri存在,java,android,file,uri,Java,Android,File,Uri,我花了几个小时试图找出我的问题,但我似乎无法找到解决办法 我正在尝试删除在尝试获取其uri时保存的图像。图像被放入内部存储器中。 我的代码是: Uri imageUri = getImageUri(this, selectedImage); File file = new File(imageUri.getPath()); if (!file.exists()) { Log.i(TAG, "nope"); else{ file.delete(); } } public Uri

我花了几个小时试图找出我的问题,但我似乎无法找到解决办法

我正在尝试删除在尝试获取其uri时保存的图像。图像被放入内部存储器中。 我的代码是:

Uri imageUri = getImageUri(this, selectedImage);
File file = new File(imageUri.getPath());
if (!file.exists()) {
    Log.i(TAG, "nope");
else{
    file.delete();
}
}

public Uri getImageUri(Context Context, Bitmap image) {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(Context.getContentResolver(), image, "Title", null);
    Log.i(TAG, path);

    return Uri.parse(path);
}
但是,file.exists()返回false,尽管我刚刚创建了图像uri。为什么会这样

更新

多亏了Shinil M S,我找到了解决问题的办法。然而,我还有一个问题,每当我删除照片时,图像就会被另一个空图像替换。我已经附上了这张照片,因为我不确定它到底是什么。我怎样才能完全摆脱它,使图像根本不出现

更新2

我已使一切按计划进行

我的代码是:

public static void deleteUri(Context context, Uri uri){

    long mediaId = ContentUris.parseId(uri);
    Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Uri itemUri = ContentUris.withAppendedId(contentUri, mediaId);

    context.getContentResolver().delete(itemUri, null, null);
}
我使用了这个人的解决方案:

试试这个

Uri imageUri = getImageUri(this, selectedImage);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

File file = new File(filePath);
if (!file.exists()) 
  Log.i(TAG, "nope");
else 
  file.delete();
试试这个

Uri imageUri = getImageUri(this, selectedImage);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

File file = new File(filePath);
if (!file.exists()) 
  Log.i(TAG, "nope");
else 
  file.delete();

尝试获取绝对路径,这可能会给出一条路径content://what
选择了图像的路径吗?
您的图像路径是什么?以及您在
File File File=新文件(imageUri.getPath())中获得的内容?尝试获取绝对路径,这可能会给出一个路径content://what 
选择了图像的路径吗?
您的图像路径是什么?以及您在
File File File=新文件(imageUri.getPath())中获得的内容?谢谢!这起作用了。我还有一个小问题,我将更新我的原始帖子。你如何将图像加载到你的imageView?你在使用库吗?我使用imageView.setBitmap()方法。谢谢!这起作用了。我还有一个小问题,我将更新我的原始帖子。你如何将图像加载到你的imageView?您在使用库吗?我使用imageView.setBitmap()方法。