我使用 DocumentFile 检查文件元数据和存在。这是这样实例化的:
DocumentFile df = DocumentFile.fromSingleUri(context, uri);
boolean exists = df.exists();
该文件的 uri 以前有效,直到我故意在文件系统中手动删除该文件以查看应用程序将如何反应。现在这个堆栈跟踪出现在应用程序中:
03-14 11:33:08.559 27087-27097/? E/DatabaseUtils: Writing exception to parcel
java.lang.IllegalArgumentException: Failed to determine if primary:Download/thefile.txt is child of primary:Download: java.io.FileNotFoundException: Missing file for primary:Download/thefile.txt at /storage/emulated/0/Download/thefile.txt
at com.android.externalstorage.ExternalStorageProvider.isChildDocument(ExternalStorageProvider.java:326)
at android.provider.DocumentsProvider.enforceTree(DocumentsProvider.java:198)
at android.provider.DocumentsProvider.query(DocumentsProvider.java:489)
at android.content.ContentProvider.query(ContentProvider.java:1017)
at android.content.ContentProvider$Transport.query(ContentProvider.java:238)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:453)
03-14 11:33:08.561 27046-27419/packagename W/DocumentFile: Failed query: java.lang.IllegalArgumentException: Failed to determine if primary:Download/thefile.txt is child of primary:Download: java.io.FileNotFoundException: Missing file for primary:Download/thefile.txt at /storage/emulated/0/Download/thefile.txt
它不会使应用程序崩溃,它只会在我每次尝试使用 DocumentFile 时生成此堆栈跟踪。所以在最坏的情况下,这只是 logcat 中的一个刺激物。
我希望FileNotFoundException
使用.exists()
. 当我检查它是否存在以避免使用不存在的文件时。
在这种情况下是否推荐使用不同的功能?