Android | obtain the sdcard (removable) path - android

I've tried a lot of method and I've read a lot of posts, without find a valid soluction.
Summing:
I try to use this https://github.com/18446744073709551615/android-file-chooser-dialog to choose a directory and use it. This code work perfectly with internal storage.
With this code: System.getenv("SECONDARY_STORAGE"), on my Asus TF201 with JellyBean and with Lollipop, I have /storage/sdcard1 and I can use this with previus code and it works.
Now, the problem.
I've tried with this (and other similar solution): How can I get external SD card path for Android 4.0+? but on LG G3 with Android 6.0 I've /mnt/media_rw/9014-4EF8 and doesn't work with previous code.
How can I obtain the external sd path? I need only to read in sd. Or can I use /mnt/media_rw/9014-4EF8?

Related

SD Card Location on Android

I'm using Bluestacks for testing my app, because I don't have Androids lying around. I'm tring to write a file to the SDCard but can't seem to figure out the path for it. I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but neither of those worked.
I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but
neither of those worked.
You should not hardcode paths. Because SD card storage location or path varies from phone to phone. SD card storage location in my phone is /storage/sdcard1
Now coming to your question,
Before API level 19, there was no official API method to store in SD card. But, many could do it using unofficial libraries or APIs.
Officially, one method (getExternalFilesDirs) was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).
File[] getExternalFilesDirs (String type)
It returns absolute paths to application-specific directories on all
shared/external storage devices where the application can place
persistent files it owns. These files are internal to the application,
and not typically visible to the user as media.
That means, it will return paths to both types Storage - Internal memory and Micro SD card. Generally, second returned path would be storage path of micro SD card(but not always). So you need to check it out by executing the code with this method.
Instead of hardcoding paths, you should use this method in your app source code to get the SD card location. Then, write files to that location.
If you want to know more about storage location or paths in Android, please go through my other answer

How to get emulated storage path on Android 6?

I want to get the emulated storage path. System.getenv("EMULATED_STORAGE_TARGET") works on previous versions i.e Android 5 and below but returns null on Android 6. I searched on google but couldn't find anything. How can I get the emulated storage path on Android 6. Any help will be appreciated. Thanks in advance!!
You appear to be referring to external storage, which on some versions of Android will be located at /storage/emulated/0/. The proper way to obtain the root of external storage, on all versions of Android, is by means of Environment.getExternalStorageDirectory().
If you find yourself using environment variables in an Android app, you are doing it wrong.

Populate listview with mp3 files found locally [duplicate]

I want to download some files and save them into the internal storage of the phone/tab. Tried on Samsung Galaxy Note 2 and Galaxt Tab 10.1. When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.
I want to get absolute path of the phoe or tab's internal storage' Absolute Path.
Does it possible, if so how?
When I use /storage/sdcard0/ on them the code runs successfully but when I use Galaxy Nexus 3 the code fails.
/storage/sdcard0/ is not internal storage. It is external storage.
(BTW, since there is no device named "Galaxy Nexus 3", I am assuming that you simply meant "Galaxy Nexus")
To find locations on internal storage for your app, use getFilesDir(), called on any Context (such as your Activity, to get a File object.
To get a location on external storage unique for your app, use getExternalFilesDir(), called on any Context (such as your Activity, to get a File object.
To get a standard location on external storage for common types of files (e.g., movies), use getExternalStoragePublicDirectory() on Environment.
To get the root of external storage, use getExternalStorageDirectory() on Environment. This, however, is considered sloppy programming nowadays, as it encourages developers to just put files in random locations.
why can't you use Environment.getExternalStorageDirectory() for getting /storage/sdcard0/ path from code ?
need to add permissions as well
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The solution found is
1st edit the emulator:
Go to Android Virtual Device Manager and then edit the emulator
Set say 100 MB for SD d card for the respected emulator, say Ok
Save and close emulator and start
It worked for me, the app is no longer giving an error and is working fine.

Android app says folder exists but I cant see it

I have an app I made on my old phone. It creates a folder called LocationTracker. When Im stepping through the code it says the folder exists. But I cant seem to browse to it when I connect my phone to the computer. I just checked my file explorer on my phone and i can browse to the folder there as well. According to that it is under the sd card, but i just cant see it on my PC.
Im rooting around in my file explorer on my phone. There appears to be 2 sd card folders. One is called sdcard1, and that seems to be my actual sd card. The other is called sd card. I think its an emulated one. I use Environment.getExternalStorageDirectory(), i thought that returned the sd card if one was available. If that is the case why is it using the emulated folder instead of the actual sd card folder? How do I fix this?
From Android documentation:
In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through
The rest of the sentence is sadly missing. However the methods are Context.getExternalFilesDirs() and Context.getExternalMediaDirs().

Why i can't copy from asset folder in "Lg g2, Android 4.4.2" well as in other mobiles?

For months the code provided in this answer was ok for me (Android - Copy assets to internal storage) to copy pdf files.....until yesterday a client report me that on his "Lg g2, with Android 4.4.2" this does'nt work....the app does'nt crash but the alert tell him "the document path is not valid"....
maybe because the mobile hasn't an external storage?
i'm not sure if the file copied well and the problem is opening the pdf file....but in others phone works ok...
EDIT 1:
Ok, I still dont knonw the reason of that on his LG G2 but, i solved this by checking if the external storage is available and writable, and if not, i copied the files into /data/data/package.name/ and then i used a content provider to read/access it....
I edited just in case this is helpfull for others
EDIT 2 (may 5th): well, the same client with the LG G2 told me that he actually has a sdcard...Reading this post (Howto avoid the "EACCES permission denied" ON SDCARD with KITKAT 4.4.2 Version. New policy from google) i believe is all because 4.4.2...so frustating...
So, my final solution was, check if the external storage is available, writable and if android API version < 19, and if not, i copied the files into /data/data/package.name/ and then i used a content provider to read/access it....like before ;)
Thx again
Possibly because 4.4 doesn't allow you to arbitrarily write to the SD card.

Categories

Resources