How to access USB Path in android? - android

I am connected android device and PC via USB cable. My Internal SD Card location Path as /mnt/sdcard. But my External USB device path as /mnt/userdata1. I am try to use this code to find only the Internal SD Card Path Environment.getExternalStorageDirectory(). I am using this code to access only in the internal SD Card path. How to access the external USB Path.
For example Screenshot is here...
Example
In this example contains Internal Memory, External SD card and USB Storage. How to find this path ( Internal Memory, External SD card and USB Storage) programmatically. In this code Environment.getExternalStorageDirectory() is viewed files from all Internal Memory Files only. So how to access others path ( External SD card and USB Storage ) Please guide me with code. Thanks..

If I understand correctly, what you are calling "external" USB path is actually the mount point for your SD card on your computer. Most likely, your SD card has label userdata1. Therefore when it's mounted on the computer, it gets /mnt/userdata1 mount point. However this is not strictly necessary and it can be any mount point at all. In fact, if you connect it to another computer, it can easily be another mount point.
Because this path is determined by the computer operating system, you'll need to find this path on your computer (note that this can be different every time you connect your phone to your PC, so you'll need to do it every time).
From your question and path structure (/mnt/userdata1) I'm guessing you're using linux or some other unix version. Therefore you could run mount on your PC to see the list of the mounted devices. For example, here's the output on my mac:
$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
/dev/disk1s1 on /Volumes/ALEKS540 (msdos, local, nodev, nosuid, noowners)
Note the last line in the output - this is my connected android phone with the SD card mounted to the computer. On macs, the mount points are created under /Volumes instead of /mnt. Other than than, ALEKS540 is the label of my SD card, hence it's mounted this way.
Internally on the phone, it's still mounted as /mnt/sdcard.
From the point of view of Android, there may be three storage types:
Internal memory it's always mounted under / on the device and contains everything except the SD card and USB storage below.
SD card - this is referred to as "external storage" and is usually mounted as /mnt/sd, but not always. Environment.getExternalStorageDirectory() will return the path of SD card mount point.
USB storage - this is only supported on very few devices (those that support USB host mode for external storage). This will be mounted somewhere under /mnt, but the exact location will vary. You will need to use Android NDK to interrogate and iterate mounted devices to find the one you're after.

Related

Android: Determine what is SD card and what is USB disk

In an Android app, I am storing files on a SD card in the device.
Occasionally the user also need to export data to a USB disk that will be attached for the occasion.
I am using Context.getExternalFilesDirs() to locate the SD card and I am using Environment.isExternalStorageRemovable() to distinguish the internal storage from the SD card.
My problem is that the USB disk, when attached, show up the exact same way as the SD card. It is included in the list returned by Context.getExternalFilesDirs() and is also "removable" according to Environment.isExternalStorageRemovable() and the path does not reveal anything.
Is there a way to distinguish an attached SD card from an attached USB drive?

Get absolute path of the storage connected via OTG

Absolute path of storage connected via OTG in Android,
I have tried using storage access framework i can get the content uri of the directory selected but i couldn't convert to absolute file:// path.
https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE
I need to store the absolute file path to scan the directories when the app is opened or when user access the folders.
As explained in SO How to get the file path from a device acting as a usb mass storage in android
this does provide the natively mounted otg storage devices, Samsung device natively mount the storage device where you can get the absolute path, but other device its not mounting natively,
I have tried out with
String sdcard_path = System.getenv("SECONDARY_STORAGE"); it won't provide the usb mounted path.
Environment.getExternalStorageDirectory() provides the internal mounted device path
/storage/emulated/0/
/sdcard/
not the otg mounted storages.

Comparing sdcard paths

Device 1
/sdcard attached to real sd card
/mnt/sdcard attached to real sd card
Device 2
/sdcard attached to internal memory
/mnt/sdcard attached to internal memory
/external_storage(something like this) to real sd card
.
.
/sdcard and /mnt/sdcard seems to have the same location. Always ?
My question is, how do I know if they attached to same directory ?
I tried new File(path1).equals(new File(path2)); but it return false. Only way I see is, create a hidden file with unique id and check the existence in both paths.
PS : I'm aware of Environment.getExternalStorageDirectory(). But I need these paths for some specific purpose.
/sdcard and /mnt/sdcard are not always the same. There are many paths that can exsist such as:
/emmc
/mnt/sdcard/external_sd
/mnt/external_sd
/sdcard/sd
/mnt/sdcard/bpemmctest
/mnt/sdcard/_ExternalSD
/mnt/sdcard-ext
/mnt/Removable/MicroSD
/Removable/MicroSD
/mnt/external1
/mnt/extSdCard
/mnt/extsd
/mnt/usb_storage <-- usb flash mount
/mnt/extSdCard <-- usb flash mount
/mnt/UsbDriveA <-- usb flash mount
/mnt/UsbDriveB <-- usb flash mount
There should be no reason that you would need a direct path hard coded to the internal or external memory when they make functions available for that. Anything that you are hard coding will be the same as what you get back from the functions that you have listed only the will be for that specific devices.
This will give you a string of the absolute path to a file on the external storage:
String myPath = sdcardEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/folder file is in/file you want path to";
And this will get you the path your applications internal storage:
this.getApplicationContext().getFilesDir()
You cannot always access the external SD card from code because of the way that newer versions of android partition itself. Here is statement directly from android dev site:
It's possible that a device using a partition of the internal storage for the external storage may also offer an SD card slot. In this case, the SD card is not part of the external storage and your app cannot access it
new File("/sdcard").getCanonicalPath().equals(new File("/mnt/sdcard").getCanonicalPath())
Omitted are exception handling and other niceties. Note, however, that ObieMD5 is correct, and you should not be doing this, as you can see from the list of paths in the answer above.
Also note that this method will only resolve symlinks; if your device uses mount instead to have the same space accessible from two locations, this method will not work.
attached to real sd card
The Android SDK, at present, has no concept of "real sd card". There is external storage. The definition of where external storage resides is up to the device manufacturer.
attached to internal memory
Those paths at best refer to external storage, and at worst do not exist.
/sdcard and /mnt/sdcard seems to have the same location. Always ?
No. Those paths may not even exist on some devices, as there is no requirement that they exist, and modern devices do not use those paths. Always use methods on Environment or Context to find locations on external storage.

How to detect if an sdcard or mount point will mount as removable storage or MTP?

When an android phone is connected to a computer with a USB cable it may mount as removable storage but some phones might mount it as mtp (media transfer protocol) is there a way to detect this in android whether a sdcard will mount as removable or mtp?
You can use
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
to see if the external storage is mounted and you can use
Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED);
to see if it is removed completely. You will find all the different cases you need in the Environment class.

Get the right external storage from Android devices

I tried to figure out the the right external storage (additional sdcard with more space ) location of an Android device.
I know from different user, that the location is not always the same.
The method Environment.getExternalStorageDirectory returns me always mnt/sdcard.
I have the following variants are reported from users:
mnt/sdcard/tflash
mnt/sdcard/external_sd
mnt/sdcard
How can I determine the real location of an external sd-card?
Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard"
No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash. Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine", at least for Android 2.x and above.
But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?
Android has no concept of "external SD", aside from external storage, as described above.
If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it.
How can I determine the real location of an external sd-card?
=> Environment.getExternalStorageDirectory() gives you the exact location of your external storage device. Path may be different because it depends on the consideration of manufacturer.
For example: I found /sdcard on some HTC devices and /mnt/sdcard on some samsung devices.
So Environment.getExternalStorageDirectory() is the correct way.

Categories

Resources