我使用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语句,即使文件是实际创建的,文件也不存在.
这是因为文件在创建时不能被generell访问,还是因为文件不可读?