Can I somehow find out which storages are currently available WITHOUT starting the document picker and let the user select a directory?
I want to display all storage roots like following:
File mainRoot = Environment.getExternalStorageDirectory();
// any root path that is available => I don't need access rights here, I just want to know which storages are available and "mounted"
DocumentFile sdCardRoot = ???;
DocumentFile usbCardRoot = ???;
DocumentFile gDriveRoot = ???;
Why?
Actually I want to know if an sd card is available before telling the user that he needs to select the sd cards root directory via the document picker...
So I want following:
check if an sd card is available
if not, do nothing
if an sd card is available, tell the user that my app needs permissions to read the sd card and ask the user to select the sd cards root directory via the storage access framework
The same I want to do for the usb stick, but there I know how to do it (there broadcast receivers exist to get notified when an usb stick is added and I can check if on is already connected as well)
You can do the following:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
//Ask for the permission
File externalRoot = Environment.getExternalStorageDirectory(); //gives the root directory of External storage
}
Edit:
Go to your .android\avd\"avd_name".avd folder , and open config.ini ; Make sure you have ,
hw.sdCard=yes
sdcard.size=100M
sdcard.path=[Path to ".android" directory]/.android/avd/Nexus_5X_API_23.avd/sdcard.img
Here's the thing I came up until now:
File root = Environment.getExternalStorageDirectory();
DocumentFile sdCardRoot = null;
File[] fs = context.getExternalFilesDirs(null);
// at index 0 you have the internal storage and at index 1 the real external...
if (fs != null && fs.length >= 2)
{
String mainPath = fs[0].getAbsolutePath();
String subPath = mainPath.replace(root.getFolder().getAbsolutePath(), "");
String pathSDCard = fs[1].getAbsolutePath().replace(subPath, "");
String relTreePath = pathSDCard.substring(1).replace("storage", "tree") + "%3A";
// this string is defined com.android.externalstorage.ExternalStorageProvider... but seems to not be accessable in code...
Uri treeUri = Uri.withAppendedPath(Uri.parse("content://com.android.externalstorage.documents"), relTreePath);
sdCardRoot = DocumentFile.fromTreeUri(context, treeUri);
}
// now you have the internal storage root in "root" - this path is directly readable via the `File` class
// and the sd card's root is in sdCardRoot and can be managed via the `DocumentFile` class...
Related
I need to create file in storage card from my Android program. I know I can create this file only in particular location like \Card\Android\data\my.application.package\. How to get this directory exact location? Code below brings path /data/my.application.package/Log and mkdirs() does not creates directories.
String path = "Log";
File root = new File(Environment.getDataDirectory().getAbsolutePath(),
"my.application.package" + File.separator + path);
boolean dirExists = true;
if (!root.exists()) {
dirExists = root.mkdirs();
}
How to get the same application path on internal storage?
You should use getFilesDir() for getting app specific internal File path and getExternalFilesDir() for getting app specific external File path.
Note that on Android 4.3 (API level 18) or lower, your app need to request storage-related permissions to access app-specific directories within external storage.
For more information see https://developer.android.com/training/data-storage/app-specific
I have write a code to find if SD card path if available which is like this
File[] paths = ContextCompat.getExternalFilesDirs(context, null);
if (paths.length > 1) {
if (paths[1] != null) {
root = paths[1].getAbsolutePath();
// for sd card
} else {
root = Environment.getExternalStorageDirectory().toString();
}
} else {
root = paths[0].getAbsolutePath();
}
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files" but I wanted to save data outside Android folder.
I have also tried to make an directory outside the Android folder.But unable to make it.I am testing in Lave phone with version Marhmallow
As you are using Marshmallow, you should be requesting runtime permissions (more details here). Despite that, you may add the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external storage access. You can find more information on how to here.
You can access the external storage path like this: Environment.getExternalStorageDirectory().getAbsoluteFile(). If you are getting FileNotFoundException, it's probably because you didn't add an additional "/" before your file. Example:
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +
"/your_file.txt");
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files"
Ok. Go ahead. You will succeed.
but I wanted to save data outside Android folder.
Well that is not possible anymore on modern Android systems.
Even inside the Android folder you can only write to mentioned private directory for your app.
If you want to write to the whole micro SD card then use the storage access framework.
This will help you
As miguelarc said:
As you are using Marshmallow, you should be requesting runtime
permissions (more details here). Despite that, you may add the
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external
storage access.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/yourdir");
if (f.mkdirs() || f.isDirectory())
Log.e("path",f.getPath());
//do what you want
}
I would like to store some cache data in the path /sdcard/Android/data/, but how can I get it?
simply use Context.getExternalCacheDir(), this is the standard way android provides for external cache, when your app uninstalled, the dir removed automatically and don' t leave any trash for the user.
the dir pattern ends like Android/data/your-package-name/cache/
but you should check null for the return value (a File object), if null, it indicates that the cache dir is not avaialbe(like sd card removed or connected to your pc, etc.)
I am doing this before saving my files to my external folder.
String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathToExternalStorage + "/" + getText(R.string.app_name));
if (!appDirectory.isDirectory() || !appDirectory.exists()) //Checks if the directory exists
appDirectory.mkdir();
I have phone (B15 CAT) with a sd card slot. When i insert a sdcard in this phone and asking for the external storage directory with :
Environment.getExternalStorageDirectory()
it always return an space on sdcard0 which is the internal memory. This memory is too small for my need.
By listing /mnt i found a mount point named /sdcard2 which is the "real" scard.
Unfortunately sdcard2 doesn't seems to be a standard and some other brand will use some other name...
Knowing that getExternalStorageDirectory() seems working as expected on phone with no sdcard slot , like nexus 4, how should i handle external storage to be sure to write on the sdcard (big space available) and not on internal memory ?
I have tried something like this :
File mnt = new File("/mnt");
File[] liste = mnt.listFiles();
boolean hassd2 = false;
for(File mount : liste) {
if(folder.getName().equals("sdcard2") {
hassd2 = true;
break;
}
}
String path = "";
if(hassd2) {
path = "/sdcard2/my/folder/"
} else {
File p = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/my/folder/");
path = p.toString();
}
It's working but only with this specific phone and others one with no sdcard slot ...
I also had the problem with the build in functions of Android in case of multiple 'external' storages mounted. I parsed the mounted directories directly from the f_stab file.
This link should give you the code you needed.
After having the mount points you could try to calculate the available space in order to decide if it is enough for your operation.
My friend and I are attempting to create an app that saves files to a device. We used this code to write to an external SD card, and it works great on his Droid X and Samsung Galaxy Tab.
Get the path to the SD card:
private static final File ROOT = Environment.getExternalStorageDirectory();
Create the folder path and files:
FileWriter fw = new FileWriter(ROOT + "/test/" + "time_frames.txt");
we are using document factory to create the documents
so you can see that we create the path then try to save to that path that was just created
File file = new File(ROOT + "/test/" + "time_frames.txt");
When I run it on my Nexus S (which does NOT have a SD card) is having trouble with the exact same code.
private static final File ROOTtest = Environment.getExternalStorageDirectory();
this returns /data
private static final File ROOT = Environment.getRootDirectory();
this returns /mnt/sdcard
private static final File intData = Environment.getDataDirectory();
this returns /system
my question is which one of these will work for devices that have SD cards and no SD cards? I have tried a lot, but trying all this stuff has really confused me. Thanks in advance
Environment.getExternalStorageDirectory() returns the path to external storage, it should work on all devices. Whether they have an actual SD card doesn't matter, and your code shouldn't care either. You need to make sure that external storage is available before you try to use it though, because it could be unmounted at any time.