skip to Main Content

I wrote program that give a file from asset folder and save it on text file ,my app work on Android 8 but when I install on Android 13 it doesn’t work , here is part of my code


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" />

Has anyone encountered this problem? , can you help me?

2

Answers


  1. With API30+ you no longer have access to the entire memory for privacy reasons, see the short overview article Scoped storage. Your string destinationPath must point to internal storage (like /storage/emulated/0/Android/data/(YOUR_PACKAGE)) or public folders (like /storage/emulated/0/Download or /storage/emulated/0/Documents) as described in the article. The path Environment.getExternalStorageDirectory() = /storage/emulated/0 in your sample doesn’t work anymore.

    Login or Signup to reply.
  2. I created simple util for cases like you’re (and my was :’D)
    You need to grand MEDIA permissions ofc (SDK 33 )

    {
                    //find external dierctories
                    val parentDirectory = "/storage/self/primary/"
                    val listOfPossibleDirectories = arrayListOf<String>(
                        "Documents/", "Download/")
                    var filePath = ""
                    var fileName: String? = null
                    val projection = arrayOf(OpenableColumns.DISPLAY_NAME)
                    val cursor: Cursor? = context.contentResolver.query(uri, projection, null, null, null)
                    cursor?.use { cursor ->
                        if (cursor.moveToFirst()) {
                            val fileNameIndex: Int = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
                            fileName = cursor.getString(fileNameIndex)
                        }
                    }
                    val file = File(context.dataDir, fileName)
    
                    var totalSpace: Long = Long.MIN_VALUE
    
                            for(i in 0..listOfPossibleDirectories.size){
                                if(File(parentDirectory+listOfPossibleDirectories[i]).isDirectory){
                                    filePath = parentDirectory+listOfPossibleDirectories[i]+fileName
                                    totalSpace = File(filePath).totalSpace
                                    if(totalSpace != 0L){
                                        break;
                                    }
                                }
                            }
    
                    return filePath
                }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search