简体   繁体   中英

Android create a new folder on internal “Device Storage”

So, I am creating an app that will be saving pictures. But I'd like to bundle all of the pictures into an easy-to-get-to folder on the main "device storage" directory.

I'd like to save to an external card, but the device I'm testing on does not have one and other devices will possibly not have them either (Eventually, I'll allow the app user to decide if they want internal or external memory).

Right now, I was trying to find the folder like this:

String directory = "/mnt/CPB";
File mainFolder = new File(directory);
if(mainFolder.exists() && mainFolder.isDirectory()){
    Toast.makeText(getApplicationContext(), "CPB Is Made", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getApplicationContext(), "CPB does not exist", Toast.LENGTH_SHORT).show();
    mainFolder.mkdir();
}

This is obviously not working. So where do I go from here?

I'd like the folder to show up on the main directory view when viewing folders or when attaching to a computer to view the directories. Also, Do I need any type of permissions in the android manifest?

But I'd like to bundle all of the pictures into an easy-to-get-to folder on the main "device storage" directory.

I am going to assume, from context, that you are referring to what the Android SDK calls external storage .

Note that while you may think that cluttering up the user's external storage with yet another top-level directory is a good idea, please do not be shocked if users disagree with your assessment.

Eventually, I'll allow the app user to decide if they want internal or external memory

My guess is that you are referring to external storage and removable storage , respectively.

I was trying to find the folder like this:

You do not have read/write access to /mnt , and the user has no access to that either.

What you appear to be aiming for is Environment.getExternalStorageDirectory() . However, you should strongly consider either Environment.getExternalStoragePublicDirectory() (for common locations, like photos and stuff) or getExternalFilesDir() on Context (for an app-specific location on external storage).

Do I need any type of permissions in the android manifest?

All locations on external storage require WRITE_EXTERNAL_STORAGE on Android 4.3 and older. Also, the locations on external storage provided by Environment require WRITE_EXTERNAL_STORAGE on Android 4.4+ as well.

However, if you use getExternalFilesDir() , you do not need WRITE_EXTERNAL_STORAGE on Android 4.4+. This is another reason to consider using that location.

Also note that WRITE_EXTERNAL_STORAGE is a dangerous permission in Android 6.0+, meaning that if your targetSdkVersion is 23+, you have to ask for it at runtime. Plus, regardless of targetSdkVersion , the user can decide to not grant you that permission. Hence, avoiding WRITE_EXTERNAL_STORAGE is generally a good idea.

main device storage is not /mnt , it's usually /storage/sdcard0 (at least on my nexus 4)

better yet, use getExternalStorageDirectory()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM