[英]Struggling with Scoped Storage - how would I write a plain text file in app directory in Java?
I am migrating our apps to API version 30, and I am totally struggling with Scoped Storage.我正在将我们的应用程序迁移到 API 版本 30,并且我完全在 Scoped Storage 中苦苦挣扎。 I would like to know, how would I write a plain text file in the app directory using Scoped Storage?
我想知道,如何使用 Scoped Storage 在 app 目录中编写纯文本文件? I am currently using FileWriter and I would like to know how I would change this to Scoped Storage.
我目前正在使用 FileWriter,我想知道如何将其更改为 Scoped Storage。 I would like my example in Java, not Kotlin, please.
我想要我在 Java 中的示例,而不是 Kotlin,请。
Use the location使用位置
context.getFilesDir()+"/YourFolderName";
Here you can store any file you want without any permission, If you want to store Document file where its publically available, Use this location, you dont need any permission over here either在这里,您可以在没有任何权限的情况下存储您想要的任何文件,如果您想将文档文件存储在公开可用的位置,请使用此位置,您也不需要任何权限
try {
inputStream = //your input stream here;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ContentResolver resolver = ctx.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "My Document name");
values.put(MediaStore.MediaColumns.MIME_TYPE,"application/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS + File.separator + "my app folder/");
}
Uri fileUri = resolver.insert(MediaStore.Files.getContentUri("external"), values);
try {
fos = resolver.openOutputStream(fileUri);
Objects.requireNonNull(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
inputStream.close();
fos.flush();
fos.close();
} catch (Exception e) {
Log.d("TAG", "Exception" + e.getMessage());
e.printStackTrace();
}
I included the code for the second method bcz i felt like "MediaStore" might confuse you!我包含了第二种方法的代码 bcz 我觉得“MediaStore”可能会让你感到困惑!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.