1

I am trying to save a file in my External SDcard but the file is getting stored in internal storage. How to store the file in the SDcard inserted

mr = new MediaRecorder();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
    Log.e("aa","sss");
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), "Recorder");
} else {
    cameraFolder= getDir("Recorder",MODE_PRIVATE);
}

if(!cameraFolder.exists()) {
    cameraFolder.mkdir();
}

fname = cameraFolder + "/myrec1.MPEG_4";
Mike
  • 4,550
  • 4
  • 33
  • 47
rishabh agarwal
  • 99
  • 1
  • 13
  • Your problem is how to find out the path to a removeble micro SD card. You know how to write a file once you have a path i think. – greenapps Jan 27 '16 at 21:33

1 Answers1

0

There is already a similar question here, you can also find more detailed information on the android docs

As suggested in a comment below I'll improve this answer.

In order to save a file to a specific position in your SD card you need to create a File object which points to that position:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/your/file/directory");
dir.mkdirs();
File file = new File(dir, "filename");

and then use a FileOutputStream to write the content.

Remember that you need to add the required permission in the manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Community
  • 1
  • 1
Marco
  • 705
  • 8
  • 28
  • A sample program to write a file to SD card can be found at: http://www.java-samples.com/showtutorial.php?tutorialid=1523 – ap6491 Jan 27 '16 at 18:37
  • the file gets save to a path when we use get External Storage method /storage/emulated/0/ but i want to save my file at location /storage/ – rishabh agarwal Jan 27 '16 at 18:49