在 Android Q 上保存 .txt 文件 [英] Save .txt file on Android Q

查看:37
本文介绍了在 Android Q 上保存 .txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用以下代码保存文本文件

I was trying to save text file using below code

try {
                FileOutputStream fos = new FileOutputStream(TXT_FILE_NAME, true);

                FileWriter fWriter;

                try {
                    fWriter = new FileWriter(fos.getFD());
                    fWriter.write(binding.tvExtractedResult.getText().toString());
                    fWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    fos.getFD().sync();
                    fos.close();
                    Toast.makeText(this, "File Saved Successfully", Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                Toast.makeText(this, "Error while saving file", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

但问题是此代码不适用于 Android Q.在此之后,我尝试搜索解决方案,我做到了

But the problem is this code doesn't work with Android Q. After this I tried to search the solution and I did this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues myContentValues = new ContentValues();
            myContentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, TXT_FILE_NAME);
            String myFolder = "Download/MY_PROJECT";
            myContentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, myFolder);
            myContentValues.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain");
            myContentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);

            Uri extVolumeUri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);

            ContentResolver contentResolver = getContentResolver();
            Uri uri = contentResolver.insert(extVolumeUri, myContentValues);

            if (uri == null) {
                Log.e(TAG, "uri is null");
                return;
            }
            Log.e(TAG, "uri=" + uri);

            try {
                FileOutputStream fos = new FileOutputStream(new File(uri.toString()));

                fos.write(binding.tvExtractedResult.getText().toString().getBytes());
                fos.close();
            } catch (Exception e) {
                Log.e(TAG, "error occurred" + e.getMessage());
                e.printStackTrace();
            } finally {
                myContentValues.clear();
                myContentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
                contentResolver.update(uri, myContentValues, null, null);
            }

        }

在上面的代码中,我得到 uri == null

In above code I'm getting uri == null

感谢您的帮助.谢谢.

推荐答案

请看一下这段代码,它可以让你保存文本文件 下面和更高Android Q 的版本.

Please have a look at this code, it will allow you to save text file below and higher versions of Android Q.

public static void saveFile(Context context, String fileName, String text, String extension) throws IOException{
    OutputStream outputStream;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        ContentValues values = new ContentValues();

        values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName + extension);   // file name
        values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain");
        values.put(MediaStore.MediaColumns.RELATIVE_PATH, DIRECTORY);

        Uri extVolumeUri = MediaStore.Files.getContentUri("external");
        Uri fileUri = context.getContentResolver().insert(extVolumeUri, values);

        outputStream = context.getContentResolver().openOutputStream(fileUri);
    }
    else {
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString() + FOLDER_NAME;
        File file = new File(path, fileName + extension);
        Log.d(TAG, "saveFile: file path - " + file.getAbsolutePath());
        outputStream = new FileOutputStream(file);
    }

    byte[] bytes = text.getBytes();
    outputStream.write(bytes);
    outputStream.close();
}

这篇关于在 Android Q 上保存 .txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆