在android中检测Sdcard?

时间:2012-01-18 07:06:49

标签: android sd-card android-sdcard

如果使用内部存储器挂载,我使用以下代码访问sdcard:

 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";

     }

但是当我使用第一个条件时找不到vidoePath。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:3)

尝试这样做:

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
}

答案 1 :(得分:3)

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

修改:根据评论,更新较新版本的答案。

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

答案 2 :(得分:1)

也请检查此代码。并且,将此代码更改为您想要的任何内容?

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;
    }

试试这个。它可能对你有帮助。

并用此检查你的SD卡检测条件。

我从Droid Nova's WebSite采用了这些方法。检查那个例子。

答案 3 :(得分:1)

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