Navdroid
Navdroid

Reputation: 4533

Detecting Sdcard in android?

I am using the following code to access sdcard if mounted else using the internal memory:

 if( android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    {       
        //File file = Environment.getExternalStorageDirectory();

        videoPath=Environment.getExternalStorageDirectory()+"/cue_learn_data/video_files/"+VIDEO_FILENAME+".mp4";

    }
    else
     {

     videoPath="/cue_learn_data/video_files/"+VIDEO_FILENAME+".mp4";

     }

But vidoePath is not found when I use first condition.Could anyone help?

Upvotes: 2

Views: 3799

Answers (4)

AAnkit
AAnkit

Reputation: 27539

   public static boolean isSDCardPresent() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

Edit : As per comment, updating answer for newer versoins.

if(isSDCardPresent() && System.getenv("SECONDARY_STORAGE") != null) { 
    Log.d("sd_status",""adcard available);
} else { 
    Log.d("sd_status", "sdcard not available"); 
}

Upvotes: 3

Praveenkumar
Praveenkumar

Reputation: 24476

Check this code also. And, change this code to whatever you want?

String Videopath = Environment.getExternalStorageDirectory() + "/whatever you want for folder name";

public void saveScreenshot() 
    {
        if (ensureSDCardAccess()) 
        {
            File file = new File(Videopath + "/" + "your file name" + "your extension");
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e("Panel", "FileNotFoundException", e);
            } catch (IOException e) {
                Log.e("Panel", "IOEception", e);
            }
        }
    }

    /**
     * Helper method to ensure that the given path exists.
     * TODO: check external storage state
     */
    private boolean ensureSDCardAccess() {
        File file = new File(mScreenshotPath);
        if (file.exists()) {
            return true;
        } else if (file.mkdirs()) {
            return true;
        }
        return false;
    }

Try this. It may be helps you.

And, check your sdcard detecting condition with this also.

I've taken these method from Droid Nova's WebSite. Check that example Also.

Upvotes: 1

droid1001
droid1001

Reputation: 41

String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED))
   {
      Toast.makeText(Context, "NO EXTERNAL STORAGE FOUND",Toast.LENGTH_LONG).show();
   }
else
   {
     //Your code                
  }

Upvotes: 1

waqaslam
waqaslam

Reputation: 68167

try doing it in this way:

String tmp = "/cue_learn_data/video_files/"+VIDEO_FILENAME+".mp4";
File myFile= new File(Environment.getExternalStorageDirectory(), tmp);

if(myFile.exists()){
    //further processing as your file exists
}
else{
    //seems like your file doesnt exist
}

Upvotes: 3

Related Questions