0
  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();

Tanveerbyn
  • 764
  • 1
  • 10
  • 25
  • 1
    You can create them for instance in the Documents folder. – blackapps Jan 28 '21 at 08:55
  • @blackapps I can't get you sir.. – Tanveerbyn Jan 28 '21 at 08:59
  • you mean I need to make my custom directory in Documents or Downloads folder? – Tanveerbyn Jan 28 '21 at 09:04
  • Not soo.. im getting confused about com.APPtop folder etc like we've done before in below android 10 – Tanveerbyn Jan 28 '21 at 09:09
  • What is com.APPtop folder? – blackapps Jan 28 '21 at 09:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227946/discussion-between-tanveerbyn-and-blackapps). – Tanveerbyn Jan 28 '21 at 09:13
  • package name folder which is found in android/data/appname – Tanveerbyn Jan 28 '21 at 09:15
  • Cant you give a full existing path? Further: what does that path have to do with Environment.getExternalStorageDirectory() or with your problem? – blackapps Jan 28 '21 at 09:17
  • Thanks for your support I found solution for it. – Tanveerbyn Jan 28 '21 at 12:20
  • 1
    Found? I gave you the solution i think. ;-) – blackapps Jan 28 '21 at 12:26
  • `File video_file = new File(uri.getPath());` What is uri? Start that piece of code please with defining that uri. Also tell the value of uri.toString(). Further you are not checking the return value of mkdir() and do not act accordingly. Further you do not have to call createNewFile() but if you do then isnt there a return value? And if not... shouldnt you check if it is created? – blackapps Feb 01 '21 at 10:54
  • `And app crashes at destination` Catch that exception and your app will not crash. – blackapps Feb 01 '21 at 10:58
  • uri is a video path that I got from response from gallery picker. and still can you elaborate this please... – Tanveerbyn Feb 01 '21 at 11:06
  • You did not tell the value i asked for. Why not? It takes more and more time now. And i want you to adapt your code before we continue. – blackapps Feb 01 '21 at 11:26
  • /storage/emulated/0/Download/. HidedFolder/myvideo1.mp4 is the value of uri – Tanveerbyn Feb 01 '21 at 11:36
  • No gallery picker will give an uri of which uri.toString() would give the value you just said. Please elaborate. Also tell the value of uri.getPath() as that is what you use in your code. – blackapps Feb 01 '21 at 11:39
  • its custom made by me can you be on point coz im getting more confuse now the main thing is that why I got this error of EACCES (Permission denied) and im not able to provide you full snippet of code coz its nested and defined in thousand of classes.. – Tanveerbyn Feb 01 '21 at 11:44
  • You could at least define that uri in your code with Uri uri = Uri.parse ( ..... ); And if you had been to the point you would have posted only code to create a file. Not a copy function where you need another file. Keep it simple. – blackapps Feb 01 '21 at 11:49
  • As I said you earlier that is happen in many cases where and that function is also used by many classes and im getting same error from every single function. – Tanveerbyn Feb 01 '21 at 11:50
  • Read this: https://stackoverflow.com/a/66366102/9917404 – Thoriya Prahalad Feb 25 '21 at 09:57

1 Answers1

2

By using below snippet my problem is solved.

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

the .getExternalStoragePublicDirectory() is deprecated.

Abdullah
  • 3
  • 1
Tanveerbyn
  • 764
  • 1
  • 10
  • 25
  • Yes, that was what i told you to use. Your own folder in an existing public directory. – blackapps Jan 28 '21 at 12:24
  • @blackapps need your help! the folder is created successfully but that is non functional like I have to edit and update, copy in it but everytime it says "java.io.IOException: Operation not permitted" – Tanveerbyn Feb 01 '21 at 08:00
  • GPUCameraRecorder: java.io.FileNotFoundException: /storage/emulated/0/Download/.HidedFolder error @blackapps still apps don't have rights to manage it – Tanveerbyn Feb 01 '21 at 10:10
  • @blackapps now check from 2nd question – Tanveerbyn Feb 01 '21 at 10:23