Accessing Removable SD card in Android Studio


For Android versions that are below Android 10, accessing the External Storage of your device is pretty straightforward. The method Environment.getExternalStorageDirectory() would return the appropriate path but now, this method is deprecated and only points to /sdcard which is not necessarily the removable secondary storage path.

You could try to hardcode the path such as mnt/externalsd, but this path varies from device to device, so it is not reliable.

From Android 10 onwards, you will need to use a Content Provider for this purpose.

What is a Content Provider?

A content provider manages access to a central repository of data which is intended to be used by other applications, using a provider client object. 

Depending on the type of files you want to access using content provider, you would either have to use :

Media Provider – audio, video and image files through MediaStoreAPI  (link https://developer.android.com/training/data-storage/shared/media

Documents provider – files such as pdfs or EUPB files for which MIME type is required

(link https://developer.android.com/training/data-storage/shared/documents-files


Here, I will stress on using a document provider.

To use a Content Provider, for any action, you need to first create a new Intent with the required action. For reading Documents, use the action ACTION_READ_DOCUMENT and for writing, use ACTION_CREATE_DOCUMENT after which you can navigate to the desired folder. 

For the Intent, you need to include setType as the MIME type of your document.

Then handle the URI using an Inputstream/Outputstream and perform your desired action using getContentProvider() in the OnActivityResult() method of your Activity as shown: 

InputStream is = getContentResolver().openInputStream(FILE_PATH_URI);

Here is an example of a simple code to read and write files from the External Storage. 

int CREATE_FILE_REQUEST_CODE = 11;
int OPEN_FILE_REQUEST_CODE = 12;

public void newDocument() {
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("text/plain");
        startActivityForResult(intent, CREATE_FILE_REQUEST_CODE);
}
public void readDocument(){
 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("text/plain");
            startActivityForResult(intent, OPEN_FILE_REQUEST_CODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent fileIntent) {
        super.onActivityResult(requestCode, resultCode, data);
        FILE_PATH = fileIntent.getData();

        if ( requestCode == CREATE_FILE_REQUEST_CODE ) {
            if (resultCode == RESULT_OK) {
                try {
                    PrintWriter writer = new PrintWriter((getContentResolver().openOutputStream(FILE_PATH)));
                    writer.write("Hello! This is a text file");
                    writer.flush();
                    writer.close();

                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
            else if(requestCode ==OPEN_FILE_REQUEST_CODE) {
	if(resultCode==RESULT_OK){
                try {
                 InputStream is = getContentResolver().openInputStream(FILE_PATH);
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String  line = reader.readLine();
                System.out.printLn( "String: " + line);
                reader.close();
            }catch (IOException e) {
                    System.out.println(e.getMessage());
            }
            }
}
}

Leave A Comment

Your email address will not be published. Required fields are marked *