API 30 - Copying non media files from external storage to internal storage

171 Views Asked by At

Does anyone have experience with copying a .TXT file in Android 11 - API 30 from external storage to the app directory (internal storage), specifically from the Download folder to storage/emulated/0/Android/data/[PACKAGE_NAME]/files/MyDir but without usage permission.MANAGE_EXTERNAL_STORAGE. If there is a possibility, can you give a concrete example, thank you.

Copy into Download folder

recovery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // copy iz int memo u download
            try {
                File sdcard = getBaseContext().getExternalFilesDir(null);
                appRecovery(
                        new File(sdcard.getAbsolutePath()),
                        new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS))));
            } catch (IOException e) {
                e.printStackTrace();
            }

            // bris check
            clearAppData();

        }
    });

Copy from Download folder

    restore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // copy
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                if (SDK_INT >= 23) {
                    if (checkPermission()) {
                        try {
                            File sdcardDic = getBaseContext().getExternalFilesDir(null);
                            File dirDic = new File(sdcardDic.getAbsolutePath() + "/Text/");
                            dirDic.mkdir();
                            File fileDic = new File(dirDic, "MyTextFile.html");
                            Toasty.success(getApplicationContext(), getString(R.string.restore_toasty_text), Toast.LENGTH_LONG).show();
                            FileOutputStream dic = null;
                            try {
                                dic = new FileOutputStream(fileDic);
                                dic.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            File sdcardAudio = getBaseContext().getExternalFilesDir(null);
                            File dirAudio = new File(sdcardAudio.getAbsolutePath() + "/Audio/");
                            dirAudio.mkdir();
                            File fileAudio = new File(dirAudio, "Audio.mp3");
                            FileOutputStream audio = null;
                            try {
                                audio = new FileOutputStream(fileAudio);
                                audio.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            // copy
                            File sdcard = getBaseContext().getExternalFilesDir(null);
                            File dir = new File(sdcard.getAbsolutePath());
                            appRecovery(new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS))),
                                    new File(dir.getAbsolutePath()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // brise sadrzaj
                        File dirHtml = new File(Environment.getExternalStorageDirectory() + "/Download/" + "/Text/" );
                        if (dirHtml.isDirectory()) {
                            String[] children = dirHtml.list();
                            for (int i = 0; i < children.length; i++) {
                                new File(dirHtml, children[i]).delete();
                            }
                        }

                        File dirMp3 = new File(Environment.getExternalStorageDirectory() + "/Download/" + "/Audio/" );
                        if (dirMp3.isDirectory()) {
                            String[] children = dirMp3.list();
                            for (int i = 0; i < children.length; i++) {
                                new File(dirMp3, children[i]).delete();
                            }
                        }

                    } else {
                        requestPermission();
                    }
                }
            }
        }
    });

}

public void appRecovery(File sourceLocation, File targetLocation)
        throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < sourceLocation.listFiles().length; i++) {

            appRecovery(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);

        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}
0

There are 0 best solutions below