Question

In my Android application, I save a bitmap file into a directory in the external storage by using the following codes:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
    final String mPath =  Environment.getExternalStorageDirectory().getAbsolutePath() + getResources().getString(R.string.record_folder);

    File dir = new File(mPath);
    if(!dir.exists()) {
        dir.mkdirs();
    }

OutputStream fout = null;
File imageFile = new File(mPath + getResources().getString(R.string.file_name));

Bitmap im = Bitmap.createBitmap(b, 0, 0, canvas.getWidth(), (int) ((float)canvas.getHeight())*600/800);

try {
    fout = new FileOutputStream(imageFile);
    im.compress(Bitmap.CompressFormat.JPEG, 97, fout);
    fout.flush();
    fout.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}
else
{

    // ?

}

If the device in which my application is installed doesn't have an external storage, what should I do?

Thanks.

Was it helpful?

Solution

Check the storage options which the android OS provides. If no external storage is mounted, I would save the file in the internal storage.

http://developer.android.com/guide/topics/data/data-storage.html

OTHER TIPS

Then you can use:

Context.getFilesDir()

to get path to file in internal memory. If your device requires presence of external memory, then you can inform about it your user and ask if storing image in internal memory is OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top