I have a requirement that I need to copy some files to the SD card programatically.
I have used
Environment.getExternalStorageDirectory()
to refer the SD card but In some devices it is referring to internal memory of the device.
Then I tried "/mnt/sdcard/" this path also still referring to Internal memory of the device.
I have done some investigation and came to know that "Environment.getExternalStorageDirectory()" will refer to internal memory of the device.
But I want to always store my files in SD card for all the devices.
I just want to know the path which should always refer to the Sd card in all the devices.
Is there any hard coded way to do this..??
Please help me.
Android doesn't know anything about the way the data is physically stored (could be a SD card, a CD, a chipset, etc). The only thing it knows is about whether the storage is "internal" or "external" (more details here).
So the way you are doing is fine: if the system gives you a path to an internal chipset when you call getExternalStorageDirectory(), this means that your physical device is built that way. There is no workaround for that.
Related
A friend of mine gave me a corrupted SD card. My windows 10 cannot detect it and on android it asks to format the corrupted SD card. Is there a way to partially or, best case, fully recover the data on this card?
Kind regards
Kurt
First thing you need to determine is if it's detected with correct capacity in disk management:
This is what to look for in Disk Management
If not then this is probably not DIY-able. A data recovery lab still could get the data by scraping off the back layer and soldering wires to a special reader.
If it is detected, my advise would be to create a sector by sector image file. Then, any decent file recovery software should be able to scan that image file as if it were a regular drive and recover the data.
To create such an image in Windows you could use Win32 Disk Imager, in Linux ddrescue. Any decent file recovery tool would allow you to create a drive image too.
I'll mention DMDE as an example as it is also a very good tool to analyze the image file with and recovers upto 4000 files for free (as long as they're in the same folder).
I am experimenting with the “Adoptable storage” introduced in Android Marshmallow and I need some guidance on the expected behaviour and how the apps should handle the “Adoptable storage”.
Format “micro SD” card as Adoptable storage by using “Format as Internal”
Once Format is done, there are 2 options given to the user as follows:
Move Now
Move Later
a. Move Now:
When this option is chosen by the user, the path returned by the getExternalFilesDirs
is “/storage/emulated/0/Android/data/PACKAGE_NAME/files” and it actually points to the file system of micro SD card.
b. Move Later:
When this option is chosen by the user, the path returned by the getExternalFilesDirs
is “/storage/emulated/0/Android/data/PACKAGE_NAME/files” and it actually points to the file system of Internal embedded memory.
In both the cases, the apps can see only one storage and the other storage is completely not accessible by the Apps to store data such as Photos, Videos, etc. Is there any way to access the storage paths of both the Internal and micro SD card when the micro SD card is formatted as “Adoptable storage” ?
Apart from this documentation ,I could not find a detailed documentation on how the apps should handle this adopted storage. Is there any API that app needs to use?
Example:
Let say, if the user phone has Internal memory of 32 GB and micro SD card of 32 GB.
If the card is formatted as “Portable storage”, both 32GB(Internal) and 32GB(micro SD card) are available to user to store data.
But If the user format the card as “Internal Memory”, the user can save data(photos/video/music,etc) only to any of this 32 GB storage location, but not to the both location. The system is providing an option of “Migrating data” between the storage locations(Internal to micro SD card and viceversa), but the apps can use only 32 GB of storage(The user effectively loses his 32 GB of storage to store Media files and can be only used to install the apps ?).
Is this the expected behaviour ?
Note: Test devices used - Moto X Play & HTC 10 - Both running Android 6.0.1
getExternalFilesDir() always points to the external storage, which may or may not be available. Use getFilesDir() to dynamically get the path to wherever the App is currently stored.
Don't store the result of this since Adoptable Storage may move your app around at any time. From the docs:
The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.
I'm developing with Android Studio on various emulated devices.
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
always returned false UNTIL I chose to emulate SD cards on the emulated devices.
But the documentation at http://developer.android.com/training/basics/data-storage/files.html specifically states that
"there are always two storage spaces and the API behavior is the same whether the external storage is removable or not."
This seems to indicate that external storage need not refer to an actual SD card and could be internal. Therefore, why would I need to size the emulated SD card greater than zero to get that code to work? What am I misunderstanding or missing?
Also, does context.getExternalFilesDir refer to the same directory's flags as Environment.getExternalStorageState?
Thank you,
Tony
You need to choose emulate SD cards to indicate the emulator that you have an external storage associated. And what you said is correct, an external storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage.
Yes, context.getExternalFilesDir returns the absolute path to the directory on the external filesystem as per the documentation, hence refers to the same directory's flags as Environment.getExternalStorageState
I have one simple question. If i have one file in system partition of android and if i copy the same file on sdcard, which will be faster to access?
Please answer this question.
Thanks in advance
The difference will be negligible. In any case, most newer devices have just one storage onboard the device, which is partitioned. In such devices, the need to access a separate SD Card goes away, as the memory is the same as the system storage.
On devices with an SD Card, there may be a very minute delay in accessing the file, but it will be negligible in pretty much any scenario.
You will not be able to really notice the access speed difference between accessing file from internal memory and SD Card.
With internal memory you will get added advantage of a security as it won't be accessible for other applications or you cannot access it by mounting.
The answer is Internal phone memory
because disk management(for phone ) is always better for internal memory as compared to SD card, So it is faster to access internal memory but you will not be able to notice the because difference is in mili seconds
With internal memory you will get advantage like user can't see your file until unless device is not rooted.
As stated, the time difference is not noticeable. However, a simple law of physics (and EE) will tell you that the longer the distance is (in this case the I/O bus), the longer it will take in time. Thus, the SDCard is slower than anything on the motherboard, even though it is measured in miliseconds.
I have found the SDCard is best used for static storage of your media files, and apps should be installed on the phone making them run as fast as possible, even though you can move some apps to the SDCard.
The speed is not noticeable unless you are coping the very large video files. The SD card speed is measured by class like:(Class 4, 6 or 10), the class 10 have higher speed as 10MB/S and class 2 have speed around 2MB/S. So, for good class of SD card, the speed gap is less. However, normally phone memory is faster than the SD card.
is it possible to have low level access in Android with Java/NDK to create for example card scanner like scandisk or card defragmenter? I move a lots of stuff around my card and it gets fragmented quickly. I have experience with FAT32 defragmenting on Windows, but I'dliek to do this on Android.
Fragmentation does not affect SD card as much as it affect a harddrive since SD card does not have spinning parts or moving heads, the difference between random access vs sequential access of an SD card is negligible.
In other words, SD card does not need defragmenting; in fact defragmenting an SD card will only lower the life span of your SD card since the memory cells of an SD card have a limit on the number of read/write that you can do on them.
And if you need to "scandisk" (or "fsck" in Unix/Linux-speak, stands for "File System ChecK"), I don't think the Android's default shell comes with fsck, but you should be able to fsck the SD card from your computer.
Maybe this is related to one of my questions.