文件创建后不存在

发布于 2025-01-15 22:26:04 字数 1258 浏览 4 评论 0原文

使用 MediaStore 我创建了名为 po.txt 的随机测试文件:

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DISPLAY_NAME, "po"); //file name
values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain"); //file extension, will automatically add to file
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS ); //end "/" is not mandatory

Uri uri = getContext().getContentResolver().insert(MediaStore.Files.getContentUri("external"), values); //important!
OutputStream outputStream = getContext().getContentResolver().openOutputStream(uri);
outputStream.write("This is menu category data.".getBytes());
outputStream.close();

File f = new File(uri.getPath());
if(f.exists()) {
    Log.e("i","never enter this code :(");
} else {
    Log.e("i","always enter this code :(");
}

文件已创建,我可以毫无问题地打开它们:

galleryviewopenedfile

但是,为了检查文件是否实际创建,根据if语句,即使文件实际创建,该文件也不存在。

这是因为该文件在创建时一般无法访问,还是由于文件不可读的正确问题?

Using MediaStore I've created random test files called po.txt:

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DISPLAY_NAME, "po"); //file name
values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain"); //file extension, will automatically add to file
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS ); //end "/" is not mandatory

Uri uri = getContext().getContentResolver().insert(MediaStore.Files.getContentUri("external"), values); //important!
OutputStream outputStream = getContext().getContentResolver().openOutputStream(uri);
outputStream.write("This is menu category data.".getBytes());
outputStream.close();

File f = new File(uri.getPath());
if(f.exists()) {
    Log.e("i","never enter this code :(");
} else {
    Log.e("i","always enter this code :(");
}

The files are created and I can open them without issues:

galleryview
openedfile

However, In order to check if the files are actually created, according to the if statement, the file do not exist even tough the files are actually created.

Is this due to this files are not generell accessable when created, or due to right issues that the files are not readable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2025-01-22 22:26:04

ContentProvider 的主要功能之一是封装数据,这样您就不必处理低级事物,例如文件。

我不确定您为什么要测试您创建的行是否存在,因为您知道只要插入调用成功,它就确实存在。 Uri.getPath 函数不会返回文件路径,而是返回解码后的 Uri 路径,从屏幕截图中可以看到,该路径类似于 "content://media/external/file /{一些数字}”

如果您想打开您创建的行的内容(返回的 Uri),您可以使用 Intent。请参阅相关开发者文档:内容提供商意图过滤器

One of the primary features of a ContentProvider is to encapsulate data so you don't have to deal with low level things, like Files.

I'm not sure why you are testing the existence of a row you created, since you know it does exist as long as the insert call succeeded. The Uri.getPath function does not return the file path, but rather the decoded Uri path, which as you can see from your screenshot is something like "content://media/external/file/{some number}".

If you want to open the content of the row you created (the Uri returned) you can use an Intent. See relevant developer documentation: Content Providers and Intents filters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文