文件创建后不存在
使用 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 :(");
}
文件已创建,我可以毫无问题地打开它们:
但是,为了检查文件是否实际创建,根据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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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