Search code examples
javaandroidandroid-11

How can I make custom folder (App folder's) in android 11


  Im facing a problem when creating app custom folder. like 

com.app and storage/.hideFolder etc.

by using some of approaches below android 11 (SDK API 30) device

it's working fine but in android 11. not able to make it im using an approach which is shown below

 public static String root= Environment.getExternalStorageDirectory().toString();
 public static final String app_hided_folder ="/.HidedAPPTop/";
 public static final String app_showing_folder ="/APPTop/";
 public static final String draft_app_folder= app_hided_folder +"Draft/";


  public static void make_directry(String path,Context context) {
    File dir = new File(path);

    if (!dir.exists())
        Toast.makeText(context,
                (dir.mkdirs() ? "Directory has been created" : "Directory not created"),
                Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(context, "Directory exists", Toast.LENGTH_SHORT).show();
}

Function calling

make_directry(Variables.app_hided_folder,getContext());

Manifest

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

 <application
     …
        android:requestLegacyExternalStorage="true"
     …
       >

2nd question

public static String root= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS ).toString();

uri is a video path that I got from response of picker.

File video_file = new File(uri.getPath());
            Log.d(Variables.tag + " new path", video_file.getAbsolutePath());
            Functions.copyFile(video_file,
                    new File(Variables.gallery_resize_video));

Function calling of copyFile

public static void copyFile(File sourceFile, File destFile) throws IOException {


        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

ERROR : new path: /storage/emulated/0/Download/STop/MP4_20210128_225711.mp4 System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/.HidedTop/gallery_resize_video.mp4: open failed: EACCES (Permission denied)

And app crashes at destination =

new FileOutputStream(destFile).getChannel();


Solution

  • By using below snippet my problem is solved.

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

    the .getExternalStoragePublicDirectory() is deprecated.