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

标签 android android-10.0

在 Android Q 中,将图片保存在应用特定的目录中,

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();

总是返回错误

这个问题。我需要使用 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我想检查避免再次下载。文件存在总是返回 false。

关于android - Android 10 如何检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57655395/

相关文章:

android - 在 Android 的 SurfaceView 中使计算和绘图独立于 CPU 速度

android - Android Q中访问/proc/net/tcp

安卓 :How to design 3D joysticks

android - 在谷歌地图 V2 中实现 getDirection

android getContentResolver().insert() 偶尔会返回 null

android - 在 Android Q 上查询 MediaStore

Android Q - 未经批准连接wifi

java - Observable.empty() 导致 java.util.NoSuchElementException : Sequence contains no elements

Android 10 无法删除多个文件(范围存储)

android - 调用 BindProcessToNetwork(network) 之后。 APP从不通过移动数据连接互联网