My tablet have Internal Storage,USB Storage,External SD card Storage.I need to access External SD card.Still I cannot get the correct answer.
-
what have you tried? If you have tried something, is it throwing any error? Which Android version is it? – pri Nov 19 '15 at 07:54
-
Are you want to get external storage path in your code or you want to excess the external storage through adb – VikasGoyal Nov 19 '15 at 07:57
-
1I tried Environment.getExternalStorageDirectory(). but this choose the USB storage. cannot access the sd card. – Gopal Nov 19 '15 at 07:59
-
@Gopal did you tried with my answer?? – Aditya Vyas-Lakhan Nov 19 '15 at 08:09
-
there is no API to get actually external storage paths prior to kitkat. http://stackoverflow.com/a/22221533 – zapl Nov 19 '15 at 08:15
-
@Lakhan no it's also access the USB storage. didn't get the external SD card – Gopal Nov 19 '15 at 09:37
5 Answers
You can access to SD card via adb like:
adb shell cd \$EXTERNAL_STORAGE
Or you may go over getExternalStorageState() .

- 876
- 2
- 11
- 34
If you have SD card, you can use Environment.getExternalStorageDirectory()
to get root path of your SD card.

- 2,021
- 2
- 16
- 21
in onActivityResult you will get data
Uri selectedImageUri = data.getData();
String path = App.getPath(getActivity(), selectedImageUri);
use this method toget your file path
@TargetApi(19)
@SuppressLint("NewApi")
public static String getPath(final Activity context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
// TODO handle non-primary volumes
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection,
selectionArgs);
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
if (uri == null) {
// TODO perform some logging or show user feedback
return null;
} else {
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = context.managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}

- 318
- 4
- 21
Try this way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText android:id="@+id/myInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" android:lines="5"
android:minLines="3" android:gravity="top|left"
android:inputType="textMultiLine">
<requestFocus />
</EditText>
<Button android:id="@+id/saveInternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save to Internal Storage" />
<Button android:id="@+id/getInternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display from Internal Storage" />
<Button android:id="@+id/saveExternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save to External Storage" />
<Button android:id="@+id/getExternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display from External Storage" />
<TextView android:id="@+id/responseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
AndroidStorageActivity.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidStorageActivity extends Activity implements OnClickListener{
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
File myInternalFile;
File myExternalFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , filename);
Button saveToInternalStorage =
(Button) findViewById(R.id.saveInternalStorage);
saveToInternalStorage.setOnClickListener(this);
Button readFromInternalStorage =
(Button) findViewById(R.id.getInternalStorage);
readFromInternalStorage.setOnClickListener(this);
Button saveToExternalStorage =
(Button) findViewById(R.id.saveExternalStorage);
saveToExternalStorage.setOnClickListener(this);
Button readFromExternalStorage =
(Button) findViewById(R.id.getExternalStorage);
readFromExternalStorage.setOnClickListener(this);
//check if external storage is available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveToExternalStorage.setEnabled(false);
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
public void onClick(View v) {
EditText myInputText = (EditText) findViewById(R.id.myInputText);
TextView responseText = (TextView) findViewById(R.id.responseText);
String myData = "";
switch (v.getId()) {
case R.id.saveInternalStorage:
try {
FileOutputStream fos = new FileOutputStream(myInternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText("");
responseText
.setText("MySampleFile.txt saved to Internal Storage...");
break;
case R.id.getInternalStorage:
try {
FileInputStream fis = new FileInputStream(myInternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
responseText
.setText("MySampleFile.txt data retrieved from Internal Storage...");
break;
case R.id.saveExternalStorage:
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText("");
responseText
.setText("MySampleFile.txt saved to External Storage...");
break;
case R.id.getExternalStorage:
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
myInputText.setText(myData);
responseText
.setText("MySampleFile.txt data retrieved from Internal Storage...");
break;
}
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
you will get output like this

- 13,409
- 16
- 61
- 96
-
3Thats's an entire app, not the answer to the question. Which is the piece of code you think solves the problem and why? How does it work? – zapl Nov 19 '15 at 08:21
its so easy to get the paths of internal and external sd card paths. for that we just need to use the reflection mehtod and no need of rooting or signing of app. Add the following permission in manifest.
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
Code:
final Class surfaceControlClass = Class.forName("android.os.storage.StorageManager");
Method method1 = surfaceControlClass.getMethod("getVolumePaths")
method1.setAccessible(true);
final Object object = context.getSystemService(Context.STORAGE_SERVICE);
String[] volumes = method1.invoke(object)

- 1