0

I am doing a OCR application in android. For that I need to store the trained data and the images in my application folder. Until now I was doing it using

public static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/NewsReader/"

and that works absolutely fine on devices that have external SD cards.

But when I try to run the same application on device which either don't have SD cards inserted and work on internal storage or device like Nexus which have only internal storage the app fails. What alternatives do I have here. I can check the state of the External storage to see if its available or not but If not then what should my app do instead of just crashing. I even tried doing getFilesDir().toString() + "/NewsReader/" but the same results.

EDIT

From some answers that I am getting my above question does not tell what my real problem is Let me clear it more.

I am doing this : public static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/NewsReader/" and it perfectly creates the folder NewsReader in the phone when I run it on the device which have external SD cards. But fails to run on the devices with only internal storage. And to add up currently I am doing all this file creating business in my splsh activity in a thread but earlier when I was doing the same code in my normal Activities OnCreate it used to run on all devices.

Yogesh D
  • 1,663
  • 2
  • 23
  • 38
  • `Environment.getExternalStorageDirectory()` will return the path of external storage. it is sdcard path in most cases. But in some phones a external storage on phone memory. And do not use harcoded paths. Use `File.Separator`. What you are doing is right – Raghunandan Apr 02 '14 at 05:43
  • If what I am doing is right then why does application crashes only on the phones which do not External SD cards..? – Yogesh D Apr 02 '14 at 05:48
  • can u please give a filename or directory name which u want to save because some special char not support as a filename in some mobile so may be that was the prob – Bhanu Sharma Apr 02 '14 at 05:48
  • @y.dixit post the stacktrace. that will give you a clue – Raghunandan Apr 02 '14 at 05:48

4 Answers4

0

read this I hope help you http://developer.android.com/guide/topics/data/data-storage.html

You can use SharedPreferences:

To save data:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

To retrieve data:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
0

It is logical that if there is no external storage than you can not store in external storage.You can check whether device has external storage.

If not then you must store data it in internal storage.

`String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        //do your code for external storage
    }else {
        //for internal storage
    }`
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
Sayem
  • 4,891
  • 3
  • 28
  • 43
  • this is a sample code for storing data in internal storage. String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); – Sayem Apr 02 '14 at 06:13
0

You must first check weather the external storage exists or not by the following code:

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    return true;
}
return false;}

You can use internal Storage instead. Internal Storage can be used by following code:

File internal_storagefile = new File("/data/YourPackageName/files/abc.jpg");

You can even get internal storage path by:

File internalPath = File("/data/" + getApplicationContext().getPackageName() + "/");

Source : Android Storage Options

Thank you.

The Great
  • 35
  • 6
0
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 1.x and 2.x. 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.

https://stackoverflow.com/a/5695129/596555
https://stackoverflow.com/a/15131810/596555

Community
  • 1
  • 1
boiledwater
  • 10,372
  • 4
  • 37
  • 38