write file to sd card and not usb storage - android

I am developing an application that needs to write files to the SD card. I am using GetExternalStoragePublicDirectory() to determine the directory to write to.
I have two phones I am developing with. On a Google Nexus S, running Android 4.0.4 (Ice cream sandwich) it is returning a directory on the SD card. However, on a Samsung Exhibit 2 running Android 2.3.5, it is writing directly to the USB storage on the phone.
Is there a way to force the SD card?
edit:
I found that getExternalStoragePublicDirectory(), and getExternalStorageDirectory() are always returning "/mnt/sdcard/". This is not actually the mount location for the sd card. On the Samsung Exhibit 2, this is the USB storage location. The card mount location is "/mnt/sdcard/external_sd". Is there a way to return this actual sd location?
I see that the camera and other apps have found a way to do it. The camera app has a "storage" setting with options "phone" and "memory card". If "memory card" is specified, images are actually stored on the sd card.

Is there a way to return this actual sd location?
No, sorry, not in the current Android SDK.
I see that the camera and other apps have found a way to do it.
Mostly, they will either be ones written by the device manufacturer, or are reading information via MediaStore, which should index both sources.

If you want to write to SD card only use
File mySdCardPath = Environment.getExternalStorageDirectory();
File dir = new File (mySdCardPath.getAbsolutePath() + "/yourDirectoryName");

Related

Can't access the secondary storage (the external removable micro sd card)

Please read my question introduction before reading the real question:
In Android there are an internal storage and an external storage.
Don't be confused, because the internal storage relates to files that are accessible only by the app, and the external storage relates to normal files that can be accessed by all apps and can be found both in the real internal storage card (the phone's sd card) and in the real external storage card (the removable micro sd card).
My path of the phone's sd card (the one that you can't remove) is: /storage/emulated/0.
My path of the micro sd card (the one that you can insert or remove) is: /storage/external_SD.
Please note: When I use Environment.getExternalStorageDirectory().getPath() i get: /storage/emulated/0.
This is the path of the real internal storage card (the phone's sd card) and not the real external storage card (the removable micro sd card).
My problem is that I can write to the "real internal storage card" (the phone's sd card) but not to the "real external storage card" (the removable micro sd card). And I need to save files on the micro sd card.
I have tried the following things:
FileOutputStream fos;
1) fos = new FileOutputStream(System.getenv("SECONDARY_STORAGE") + "/test.txt");
2) fos = new FileOutputStream("/storage/external_SD/test.txt");
In both ways, I put the path of the micro sd card at the parameter of the FileOutputStream so it will save the file 'test.txt' on the micro sd card, but my app crashes and nothing happens!
In addition, I tried also to create a folder, but it didn't work.
However, when I put the path of the phone's sd card, it works!
And also folder creation works.
But I need to write files on the micro sd card.
Please note the following things before giving me an answer:
1) I put the following permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2) I had tried disconnecting the cable from my computer before I tried the app.
3) I tried disabling debugging mode.
4) I tried another micro sd card.
5) My smartphone is LG Optimus L7 (LG P714).
6) I have Android KitKat (4.4.2).
7) When I tried the app on another smartphone (Samsung Galaxy S3) it still crashed when I saved files on the micro sd card, but when I saved a folder it succeeded!
Does anyone know what is the problem?
My problem is that I can write to the "real internal storage card" (the phone's sd card) but not to the "real external storage card" (the removable micro sd card)
That's been the case since Android 4.4, released about four years ago.
But I need to write files on the micro sd card.
You can use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() on Context. Note the plural form of these method names. If these return 2+ items, the second and subsequent ones are locations on removable storage, unique to your app, that you can read from and write to.
Alternatively, use the Storage Access Framework and let the user choose where to put the content, where you use the resulting Uri with ContentResolver and openOuptutStream() to write to that location. This would allow the user to work with external storage, removable storage, plus locations supported by third-party apps (Google Drive, SMB/CIFS file servers, etc.).

Where is the code for SD card storage option in CyanogenMod's camera app?

I'm using CyanogenMod 10.2.1 on an Xperia ZR. This device has an emulated SD card and a removable SD card. The stock camera app has an option to store images in the emulated or the removable SD card (see screenshots below).
I'm writing a camera app and I want to do the same thing, so I looked at the code of the camera app in Android's source and in CyanogenMod's source. The first uses Environment.getExternalStoragePublicDirectory() and the second Environment.getExternalStorageDirectory(). Neither of those seem to be dynamic, so where does the setting item come from?
Here are some screenshots showing the storage option:
All in all, I want to find out whether those apps use the techniques in Find an external SD card location to access secondary storage.

Android External Storage among mobile brands

I am facing interesting problem. I have discovered, that Samsung in it's own Android phones has two implementations of storage.
1st: /sdcard - is internal memory and /sdcard/external_sd is actually inserted memory card
2nd: /storage/sdcard0 - is internal memory and /storage/ExtSdCard is actually inserted memory card.
My app needs data to be stored in sd card, so I am facing problem, how to determine, which folder might be sd card link or not. Do you know situation of any other Android maker (LG, HTC, Sony), how to cope with external SD cards and how they are visible in android system?
It is really so simple. When you consider your samsung mobiles it is the internal storage that acts as your sdcard.
if you are looking for to save a file in Sdcard, you have to stop hard coding the path like,
/mnt/sdcard.. and so.
instead use, Environment.getExternalStorageDirectory();
This will return the path to your sdcard straight away.
And from there, add Environment.getExternalStorageDirectory()+File.separator+"your directoryname";
And this works for all brands not unique to Samsung.

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.

Android. No SD card available

My friend is testing my application which uses the SD card to store some settings. He has a Samsung Galaxy S2 but he just told me that he hasn't got an SD card in his device. It seems that the device created a folder in the phone's memory which simulates the existence of an SD card and that is where all app files that used the Sdcard are stored.
Is this a feature available for all Android devices? Should I consider the fact that there is no SD card on a device or should I not bother? Not sure if I should check for SD card availability in my app or not.
P.S. I've just noticed that the same goes for the emulator if I don't specify memory for the SD card.
You definitely should check for sdcard availability. On some devices it might work (as you said), but on some not, and you could get a FileNotFoundException.
So it's worth checking.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.toString().equals(
state.toString())) {
//////then do your work here////////
}
another solution: https://stackoverflow.com/a/7429264/6451573

Categories

Resources