0

I have built an android app , i have implemented the save option , but it saves in my internal memory, how can i save it in my external memory ?

my code for save ---

else if(view.getId()==R.id.save_btn){
        //save drawing
        AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
        saveDialog.setTitle("Save drawing");
        saveDialog.setMessage("Save drawing to device Gallery?");
        saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                //save drawing
                drawView.setDrawingCacheEnabled(true);
                //attempt to save
                String imgSaved = MediaStore.Images.Media.insertImage(
                        getContentResolver(), drawView.getDrawingCache(),
                        UUID.randomUUID().toString()+".png", "drawing");
                //feedback
                if(imgSaved!=null){
                    Toast savedToast = Toast.makeText(getApplicationContext(), 
                            "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                    savedToast.show();
                }
                else{
                    Toast unsavedToast = Toast.makeText(getApplicationContext(), 
                            "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                    unsavedToast.show();
                }
                drawView.destroyDrawingCache();
            }
        });
        saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        saveDialog.show();
    }
user3104665
  • 31
  • 1
  • 4

1 Answers1

1

First make sure you include the proper permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This will give you wirte and read permissions. Next, check whether you can use external storage:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
}

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

Now choose whether you want to save to a public directory:

public File getPublicStorageDir(String directory) {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), directory);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

Or private directory:

public File getPrivateStorageDir(Context context, String directory) {
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), directory);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

Once you have the file object with the directory you can actually save:

private void saveToFile(File directory, String toWrite) {
    File file = new File(dir, "myData.txt");
    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.print(toWrite);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

Alternatively, you can save an image directly:

private void saveImage(Bitmap finalBitmap, File dir, String fileName) {
    File file = new File (dir, fileName);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

Hope this helps!

References:
http://developer.android.com/training/basics/data-storage/files.html
Write a file in external storage in Android
Android saving file to external storage

Community
  • 1
  • 1
Toguard
  • 437
  • 3
  • 9
  • thanks but could you please tell me where should i put your code? please take a look in my code and then suggest me where i should edit – user3104665 Feb 14 '14 at 17:57
  • Sorry, just changed this, must have copied the incorrect permission. "However, if your app uses the WRITE_EXTERNAL_STORAGE permission, then it implicitly has permission to read the external storage as well." Ref: http://developer.android.com/training/basics/data-storage/files.html#GetWritePermission Nice catch! – Toguard Feb 14 '14 at 18:23
  • 1
    `return (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state));` – m0skit0 Feb 14 '14 at 18:28
  • As of how to implement, these are all functions that can be called any time you need them. In your case would be when the user taps on the button. Just make sure to call them in the proper order (check SD availability, create a directory and finally save the file). – Toguard Feb 18 '14 at 19:04