Accessing SD Cards for storage

edited March 2017 in Android Mode

This could go in the code in code sharing but is Android only so might be better here?

Basically I have been trying to find ways of making use of my physical SD card, as the default seems to always be the internal emulated SD (apparently for android historical reasons). This seems to be a common problem for us amateur developers if the number of questions about it at Stack Overflow is anything to go by! Anyhow... this is my approach which seems to work, so thought I'd share. Dependent on API level it gets the file path(s) for your sd cards and converts them to strings. The result is actually to a file area specific to your app (for private data) and here I simply text() these to the display in draw(), but it is now easy to extract the first part of the string(s) which is the top level path to the sd cards themselves. On my LG phone these are /storage/emulated/0 and storage/external_SD/ ... on other phones these will be different and on oldish phone will probably be /mnt/...

Anyway, hope it may be of some use to some of you.


import android.os.Environment;
import android.os.Build ;
import android.content.Context;

Activity activity ;
Context context ;

File SDcard ; //emulated .... e.g. /storage/emulated/0
File[] SDcards ; 
String sdPath0, sdPath1 ; // for emulated & external SD possibilities

void setup() {
  Activity activity = this.getActivity();
  Context context = activity.getApplicationContext();

  // get files paths for SD cards(s) and convert to Strings
  if (Build.VERSION.SDK_INT >= 19) { 
    //from API19 (4.4 KitKat) get array of filepaths for all connected SD cards (and other media?)
    //1st path returned is emulated primary SD, 2nd path is real external SD, etc.
    SDcards = context.getExternalFilesDirs(null);
    sdPath0 = SDcards[0].toString() ;
    sdPath1 = SDcards[1].toString() ;
  } else if (Build.VERSION.SDK_INT >= 8) {
    //from API8 you can only get the file path of the primary SD card
    //(could be emulated or real)
    SDcard = context.getExternalFilesDir(null);
    sdPath0 = SDcard.toString() ;

  fill(0) ;

void draw() {
  if (Build.VERSION.SDK_INT >= 19) { 
    text ("sdCard0 file path = \n\n " + sdPath0 + "\n\n sdCard1 file path = \n\n " + sdPath1, 0, 0, width, height) ;
  } else if (Build.VERSION.SDK_INT >= 8) {
    text ("sdPath0 file path = \n\n"  + sdPath0, 0, 0, width, height) ;
Sign In or Register to comment.