gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:05:44 27 4
gpt4 key购买 nike

在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com