Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何检查内部和外部存储(如果存在)_Android_Storage - Fatal编程技术网

Android 如何检查内部和外部存储(如果存在)

Android 如何检查内部和外部存储(如果存在),android,storage,Android,Storage,从实用角度看,我如何知道android中是否有内部和外部存储?有人知道如何检查内部和外部存储吗 提前谢谢中已经解释过了 代码取自文档 boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state

从实用角度看,我如何知道android中是否有内部和外部存储?有人知道如何检查内部和外部存储吗

提前谢谢

中已经解释过了

代码取自文档

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

如果有人在搜索这个,我会让它工作。。。。这会有帮助的:D

try {
            File dir = new File("/mnt/");
            File[] dirs = dir.listFiles();
            for (File _tempDIR : dirs) {
                String sdCard = _tempDIR.getAbsolutePath().toString();

                File file = new File(sdCard + "/"
                        + Environment.DIRECTORY_DOWNLOADS);
                File[] files = file.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        String _temp = files[i].getAbsoluteFile().getName()
                                .toString();/*Your code, and what you want to find, from all the Sdcard, internal and external. Everything mounted will be found :D*/
试试看{
File dir=新文件(“/mnt/”);
File[]dirs=dir.listFiles();
for(文件_tempDIR:dirs){
字符串sdCard=_tempDIR.getAbsolutePath().toString();
文件文件=新文件(SD卡+“/”
+环境。目录(下载);
File[]files=File.listFiles();
如果(文件!=null){
对于(int i=0;i
我为检查存储状态编写了一个小类。也许它对您有一些用处

更新:清理代码,删除注释并使类保持静态

与前面的答案相比,其中的代码简化了一点:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

有点离题,但是我该如何使用这个类呢?没有构造函数!@jesses.co.tt就像这样:
StorageHelper=newstoragehelper()
Hmm…这就是我尝试过的,但它给了我错误…我最终只是编写了一个默认构造函数,它很好…我的编译设置中可能有什么东西?无论如何,谢谢!代码片段
if(state.equals(Environment.MEDIA_MOUNTED)){…}else if(state.equals(Environment.MEDIA_MOUNTED)| | state.equals(Environment.MEDIA_MOUNTED_READ_)){
.else if(state.equals(Environment.MEDIA_MOUNTED)|做什么?
else if(state.equals(Environment.MEDIA_MOUNTED)| state.equals(Environment.MEDIA_MOUNTED)|只读)
应该是
else if(state.equals(Environment.MEDIA|.equals(Environment.MEDIA_READ-ONLY)
您不应该硬编码任何外部sd卡路径,因为路径没有标准。每个供应商都可以使用自己的路径标准。路径标准有很多变体,如三星galaxy系列使用的
“/mnt/sdcard/external\u sd”
”,LG使用的
“/storage/external\u sd”
“/storage/ext\u sd”
由HTC One Max等使用。请改用
环境.getExternalStorageDirectory().getPath()
File f = new File("/mnt/sdcard/ext_sd");
    if (f.exists()) {
        // Do Whatever you want sdcard exists
    }
    else{
        Toast.makeText(MainActivity.this, "Sdcard not Exists", Toast.LENGTH_SHORT).show();
    }
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}