在API33中的Android studio中写入外部存储

问题描述 投票:0回答:1

我编写了一个程序,从资产文件夹中提供一个文件并将其保存在文本文件中,我的应用程序在 Android 8 上运行,但当我安装在 Android 13 上时它不起作用,这是我的代码的一部分


final File FolderProgramSample = new File(Environment.getExternalStorageDirectory(), "Sample");

String assetFileName1 = "keys";

copyFileFromAssetsToStorage(getApplicationContext(), assetFileName1, FolderProgramSample.toString());


public static void copyFileFromAssetsToStorage(Context context, String assetFileName, String destinationPath) {
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            // Open the asset file
            inputStream = context.getAssets().open(assetFileName);
            File destinationDirectory = new File(destinationPath);
            if (!destinationDirectory.exists()) {
                destinationDirectory.mkdirs();
                if (destinationDirectory.mkdirs()) {
                    Log.d("destinationFile", "Directory created successfully");
                } else {
                    Log.e("destinationFile", "Failed to create directory");
                }
            }

            File destinationFile = new File(destinationDirectory, assetFileName);
            outputStream = new FileOutputStream(destinationFile);

            // Copy the file
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            // Close the streams
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }enter code here`

And my manifest file is:
`<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.RECORD_AUDIO" />

有人遇到过这个问题吗? ,你能帮我吗?

android android-studio storage
1个回答
0
投票

使用 API30+,出于隐私原因,您无法再访问整个内存,请参阅简短概述文章此处。您的字符串

destinationPath
必须指向
internal storage
public folders
,如文章中所述。您的示例中的路径
/storage/emulated/0
不再起作用。

© www.soinside.com 2019 - 2024. All rights reserved.