Android 10 如何检查文件是否存在

Andriod 10 How to check file exist

在AndroidQ中,将图片保存在app-specific目录下,

path like = /data/user/0/xxx.xxx.xxx/files/phone/abc.jpg

没有保存在外存中,使用Device FileExplorer查看, 需要检查文件是否存在,避免再次下载 , 但在 Android Q file.exist() 中不起作用

File newFile = new File(path);
newFile.exists();

总是return错误

这个问题。我需要使用 MediaStore 或 SAF 来解析它。 或其他功能来检查它。

如果我使用MediaStore 来检查。使用内容解析器。可能是这样的:

public void getPhotoCursor(Uri uri) {
  Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
  try {
    if (cursor != null && cursor.moveToFirst()) {
        String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

    }
  } finally {
     cursor.close();
  }
}

但我无法获取 Uri 表单应用程序特定的目录。如果我得到 Uri,如何使用文件描述符来检查。 或使用 SAF 检查。

 File testFile = new File(getExternalFilesDir()+"phone", "abc.jpg");
 FileProvider.getUriForFile(,,testFile);

 Intent testIntent = new Intent(Intent.ACTION_VIEW);
 testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 testIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 testIntent.setDataAndType();
 startActivity(testIntent);

在ActivityResult中检查一下

如有帮助,我们将不胜感激

是我的错,每次打开APP我都会删除APP专用的所有.jpg。 所以进入APP我想检查避免再次下载。文件总是存在 return false.