简体   繁体   English

苦苦挣扎于 Scoped Storage - 如何在 Java 的 app 目录中编写纯文本文件?

[英]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.

相关问题 Java:如何将格式化输出写入纯文本文件 - Java: how to write formatted output to plain text file 如何使用 Scoped Storage 访问特定文件夹并将文件写入其中 - How to gain access to a specific folder and write a file into it using Scoped Storage Java如何将资源(内部文本文件)写出到目录中? - Java How do I write a resource (internal text file) out to a directory? 如何通过使用即将推出的 Scoped 存储在外部存储中通过 Room 编写 SQLite 文件? - How to write SQLite file via Room in external storage, by using upcoming Scoped storage? 如何将其写入 Java 中的文本文件? - How can I write it to a text file in Java? 如何使用 java 编辑文本文件中的特定行? - How would I edit a specific line in a text file with java? Java - 如何将文件写入指定的目录 - Java - how do I write a file to a specified directory 如何在Android上的外部存储设备上将文本写入文件? - How to write text to a file on external storage on android? 如何在 DCIM 目录 (Scoped Storage) Android Q 中创建隐藏文件夹 ieMyFolder [(dot)MyFolder] - How to create a hidden folder i.e .MyFolder [(dot)MyFolder] in DCIM directory (Scoped Storage) Android Q 如何编写用于解析纯文本文件的 ANTLR 语法 - how to write ANTLR grammar for parsing plain text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM