EngEl
EngEl

Reputation: 19

Cannot add file to specific folder location for Android 10

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

Upvotes: 0

Views: 336

Answers (1)

private static
private static

Reputation: 770

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

Upvotes: 0

Related Questions