kinsley kajiva
kinsley kajiva

Reputation: 1860

how to create a folder internal storage in android lollipop

i have tried most of the code i have found on stack over flow read the devlopment documentation but i still fail to create a folder and a file on internal storage in android lollipop i have not tried on lower api but i even tried those internal persmission declaration in manifest. the below sample of the code i tried do not even work for my situation:

 String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
            File mFolder = new File(path);
            if (!mFolder.exists()) {
                mFolder.mkdir();
            }
            File Directory = new File("/sdcard/myappFolder/");
            Directory.mkdirs();
    i tried this also below:



     File myDir = context.getFilesDir();
        // Documents Path
        String documents = "documents/data";
        File documentsFolder = new File(myDir, documents);
        documentsFolder.mkdirs(); 





 String publicC = "documents/public/api.txt" ;
        File publicFolder = new File(myDir, publicC);
        publicFolder.mkdirs();
and then this as well:
ContextWrapper contextWrapper = new ContextWrapper(
                getApplicationContext());
        File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
        myInternalFile = new File(directory, filename);

Upvotes: 1

Views: 974

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007533

First, I recommend you read more on what the following terms mean with respect to Android app development:

With that as background, let's review what you did:

Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";

Never use getDataDirectory(). I have no idea where this code would point to.

File Directory = new File("/sdcard/myappFolder/");

You do not have arbitrary read/write access to removable storage, and removable storage may not be found at that location anyway.

File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);

This code is fine. However, it points to internal storage, and on Android devices, you cannot see internal storage very readily. That too is fine, as developers should be used to the idea that they cannot see everything that their code affects. You might consider writing test cases to confirm that your directory was created.

String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);

This points to nowhere. Always use some method to derive the base path.

ContextWrapper contextWrapper = new ContextWrapper(
           getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);

The ContextWrapper is useless. filepath needs to be a simple directory name. You could simplify this as:

File directory = getDir(directoryName, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);

Then this code is also fine. It too points to internal storage, and therefore you will not be able to examine it directly. Once again, write test code to confirm that the directory was created as you expect.

Upvotes: 2

Related Questions