0

I am trying to create a PDF file and add it to the internal storage of the android phone. I want to create a directory for my app outside the app data folder so the phone user could access it from its Files system app.

The code I am using to create the folder is the following :

 File myDir = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "MyApp");
        if(!myDir.exists()) {
            myDir.mkdir();
        }
        File file = new File(myDir, customer+"_"+date+".pdf");

How can I fix this problem please?

Okay this code leads me to save the file in this location "/storage/emulated/0/Android/data/com.example.myapplication/files/Download/MyApp"

I want to move it to /storage/emulated/0/MyApp

I used to write this code, but now that 'getExternalStorageDirectory' is deprecated

File externalStorageDirectory = Environment.getExternalStorageDirectory();
File dir = new File(externalStorageDirectory, "MyApp"); 
if (!dir.exists()) { 
    dir.mkdir(); 
}
EngEl
  • 19
  • 1
  • 9
  • Besides that you should have `if (!myDir.mkdir()) return;` your code is ok. So what is the problem,? – blackapps Nov 12 '21 at 19:26
  • Why are you talking both about internal and external storage? Confusing! And why did you mention downloading? Is that relevant? And a FileNotFoundException which cannot be produced by the code you posted. Confusing! – blackapps Nov 12 '21 at 19:28
  • The problem is that I want to create the folder "MyApp" in internal storage not in Android/data/app folder I want to access "/storage/emulated/0" and then create the directory to add the folder When I replace the directory with /storage/emulated/0, I get FilenotFoundException (Android 10) – EngEl Nov 12 '21 at 19:29
  • Which code will give that exception? Not the code you posted. – blackapps Nov 12 '21 at 19:31
  • @blackapps I want to create a directory first in internal storage and then add a file to it. I want the phone user to be able to access this file – EngEl Nov 12 '21 at 19:31
  • "/storage/emulated/0" this location is giving me the File not found – EngEl Nov 12 '21 at 19:31
  • I read about the new restrictions in android 10 concerning root folder and storage folder – EngEl Nov 12 '21 at 19:32
  • `I want to create the folder "MyApp" in internal storage not in Android/data/app folder ` Sorry, besides that you fail to mention full path, that IS internal storage. AND external. Full path: `/storage/emulated/0/Android/data/app folder/files/MyApp`. – blackapps Nov 12 '21 at 19:34
  • `"/storage/emulated/0" this location is giving me the File not found ` That is just a string. Which code would produce the exception? You did not tell. – blackapps Nov 12 '21 at 19:35
  • And please adapt the mkdirs calls first. Dont talk about not beeing able to create a file where you cannot even create the folder where you wanna put a file in. Now you try to create a file in a folder that does not exist. – blackapps Nov 12 '21 at 19:37
  • Now for the third time. Adapt the code where you call mkdir(). And tell what your problem is then. It does not matter that that function is deprecated. – blackapps Nov 12 '21 at 19:47
  • https://stackoverflow.com/questions/58379543/cant-create-directory-in-android-10 for a more modern solution – private static Nov 12 '21 at 20:57

1 Answers1

0

I did not try the following codes, but I used them a while ago in an app:

String path = new File(Environment.getExternalStorageDirectory() + "/My Folder").getPath();
createNewFolder ( path );

The createNewFolder method creates a folder and displays a toast if created, and displays another toast if the folder name already exists:

private void createNewFolder(String path)
    {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
            Toast.makeText( context , "Folder created" , Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText( context , "Folder already exists" , Toast.LENGTH_SHORT).show();
        }
    }
private static
  • 745
  • 8
  • 17