1

I have a task to save the .txt in external SDCard(Not Device memory). I tried in all ways. I can't get the required output. The folder created and saved in internal device sdcard only. I used the following code.

 private File mPath = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Research/"+filname+".txt"); 

I also put the following code in Manifest file

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

But it is not working. Kindly help me. Thanks in advance.

8
  • "it is not working" => LogCat or at least actual/expected output. Without that, we would just be guessing what's wrong. Commented Sep 23, 2014 at 8:44
  • what kind of error you get please see logcat and tell us Commented Sep 23, 2014 at 8:44
  • you can use fileObject.mkdir(); or fileObject.mkdirs(); to create new folder/folders Commented Sep 23, 2014 at 8:57
  • I didn't get any errors. It just create folder in device memory only not in Removable sdcard. Commented Sep 23, 2014 at 10:21
  • How can you understand the folder is created in internal device sdcard and not in external sdcard? Commented Sep 23, 2014 at 10:47

4 Answers 4

1

There is no way you can determine the path to removable media (Micro SD card) reliably on Android. So stop trying. Better ask the user to indicate that path.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok sir. If the device memory full, Is that possible to store data in sdcard or not. Client asks me like that.
Yes if the device memory is full you can store data on removable media if there is removable media and the removable media is not full and the removable media is writable which is mostly not the case under kitkat. But why a new question? Please react to my answer first.
how we can access the removable media?
1

Here is the solution

Environment.getExternalStorageDirectory() 

Above code return "/storage/sdcard0", But if you want to make a folder on external SD Card you need "/storage/sdcard1". so try to do something like this

private File mPath = new File(Environment.getExternalStorageDirectory().replace("0", "1") .getAbsolutePath() + "/Research/"+filname+".txt"); 

Comments

0

USE mkDir() or mkDirs() to create new Folder/Folders:

private File mPath = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Research/"); mPath.mkDir(); mPath = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Research/"+filname+".txt"); 

Comments

0

I am not sure this can help you but I had a same problem and this code solved it. Create this private class:

private class SingleMediaScanner implements MediaScannerConnectionClient { private MediaScannerConnection mMs; private File mFile; public SingleMediaScanner(Context context, File f) { mFile = f; mMs = new MediaScannerConnection(context, this); mMs.connect(); } @Override public void onMediaScannerConnected() { mMs.scanFile(mFile.getAbsolutePath(), null); } @Override public void onScanCompleted(String path, Uri uri) { mMs.disconnect(); } } 

After creating the file call this:

new SingleMediaScanner(MainActivity.this, file); 

I hope this can help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.