android-以编程方式检查SD卡是否可用

我的应用程序仅适用于具有SD卡的手机。 因此,我想以编程方式检查SD卡是否可用以及如何找到SD卡的可用空间。 可能吗?

如果是,该怎么办?

naresh asked 2020-01-23T02:58:29Z

11个解决方案

126 votes

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable();

if(isSDSupportedDevice && isSDPresent)

{

// yes SD-card is present

}

else

{

// Sorry

}

Paresh Mayani answered 2020-01-23T02:58:37Z

14 votes

接受的答案对我不起作用

Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

如果设备具有内置存储,则返回true;否则,返回true。我的解决方案是检查外部文件目录数,如果有多个,则设备有sdcard。它可以正常工作,我已经在几种设备上对其进行了测试。

public static boolean hasRealRemovableSdCard(Context context) {

return ContextCompat.getExternalFilesDirs(context, null).length >= 2;

}

Jemo Mgebrishvili answered 2020-01-23T02:59:02Z

13 votes

按照“使用外部存储”中的说明使用StatFs。

要获得外部存储上的可用空间,请使用StatFs:

// do this only *after* you have checked external storage state:

File extdir = Environment.getExternalStorageDirectory();

File stats = new StatFs(extdir.getAbsolutePath());

int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize();

Philipp Reichart answered 2020-01-23T02:59:27Z

5 votes

我写了一个小类来检查存储状态。 也许对您有用。

import android.os.Environment;

/**

* Checks the state of the external storage of the device.

*

* @author kaolick

*/

public class StorageHelper

{

// Storage states

private boolean externalStorageAvailable, externalStorageWriteable;

/**

* Checks the external storage's state and saves it in member attributes.

*/

private void checkStorage()

{

// Get the external storage's state

String state = Environment.getExternalStorageState();

if (state.equals(Environment.MEDIA_MOUNTED))

{

// Storage is available and writeable

externalStorageAvailable = externalStorageWriteable = true;

}

else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))

{

// Storage is only readable

externalStorageAvailable = true;

externalStorageWriteable = false;

}

else

{

// Storage is neither readable nor writeable

externalStorageAvailable = externalStorageWriteable = false;

}

}

/**

* Checks the state of the external storage.

*

* @return True if the external storage is available, false otherwise.

*/

public boolean isExternalStorageAvailable()

{

checkStorage();

return externalStorageAvailable;

}

/**

* Checks the state of the external storage.

*

* @return True if the external storage is writeable, false otherwise.

*/

public boolean isExternalStorageWriteable()

{

checkStorage();

return externalStorageWriteable;

}

/**

* Checks the state of the external storage.

*

* @return True if the external storage is available and writeable, false

* otherwise.

*/

public boolean isExternalStorageAvailableAndWriteable()

{

checkStorage();

if (!externalStorageAvailable)

{

return false;

}

else if (!externalStorageWriteable)

{

return false;

}

else

{

return true;

}

}

}

kaolick answered 2020-01-23T02:59:47Z

4 votes

我对其进行了修改,以便在存在SD卡的情况下在其中设置路径。 如果没有,它将设置在内部目录中。

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)

{

path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder";

}

else

{

path = theAct.getFilesDir() + "/GrammarFolder";

}

ArdaA answered 2020-01-23T03:00:07Z

4 votes

您可以检查像这样的外部可移动SD卡是否可用

public static boolean externalMemoryAvailable(Activity context) {

File[] storages = ContextCompat.getExternalFilesDirs(context, null);

if (storages.length > 1 && storages[0] != null && storages[1] != null)

return true;

else

return false;

}

正如我测试过的那样,这非常有效。

Jaura answered 2020-01-23T03:00:31Z

3 votes

void updateExternalStorageState() {

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

mExternalStorageAvailable = mExternalStorageWriteable = true;

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

mExternalStorageAvailable = true;

mExternalStorageWriteable = false;

} else {

mExternalStorageAvailable = mExternalStorageWriteable = false;

}

handleExternalStorageState(mExternalStorageAvailable,

mExternalStorageWriteable);

}

Umesh answered 2020-01-23T03:00:47Z

1 votes

这个简单的方法对我有用。 在所有类型的设备中测试。

public boolean externalMemoryAvailable() {

if (Environment.isExternalStorageRemovable()) {

//device support sd card. We need to check sd card availability.

String state = Environment.getExternalStorageState();

return state.equals(Environment.MEDIA_MOUNTED) || state.equals(

Environment.MEDIA_MOUNTED_READ_ONLY);

} else {

//device not support sd card.

return false;

}

}

Kishan Vaghela answered 2020-01-23T03:01:06Z

0 votes

我创建了一个类来检查SD卡上的文件夹是否可用:

public class GetFolderPath {

static String folderPath;

public static String getFolderPath(Context context) {

if (isSdPresent() == true) {

try {

File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName");

if(!sdPath.exists()) {

sdPath.mkdirs();

folderPath = sdPath.getAbsolutePath();

} else if (sdPath.exists()) {

folderPath = sdPath.getAbsolutePath();

}

}

catch (Exception e) {

}

folderPath = Environment.getExternalStorageDirectory().getPath()+"/FolderName/";

}

else {

try {

File cacheDir=new File(context.getCacheDir(),"FolderName/");

if(!cacheDir.exists()) {

cacheDir.mkdirs();

folderPath = cacheDir.getAbsolutePath();

} else if (cacheDir.exists()) {

folderPath = cacheDir.getAbsolutePath();

}

}

catch (Exception e){

}

}

return folderPath;

}

public static boolean isSdPresent() {

return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

}

}

UchihaSasuke answered 2020-01-23T03:01:26Z

0 votes

** i fixed this with help of @Jemo Mgebrishvili answer**

即使存在sd卡且处于弹出状态,此功能也可以完美运行

if (ContextCompat.getExternalFilesDirs(this, null).length >= 2) {

File[] f = ContextCompat.getExternalFilesDirs(this, null);

for (int i = 0; i < f.length; i++) {

File file = f[i];

if(file!=null && i ==1)

{

Log.d(TAG,file.getAbsolutePath()+ "external sd card available");

}

}

} else {

Log.d(TAG, " external sd card not available");

}

rajesh vinew answered 2020-01-23T03:01:46Z

0 votes

科特林

fun Context.externalMemoryAvailable(): Boolean {

val storages = ContextCompat.getExternalFilesDirs(this, null)

return storages.size > 1 && storages[0] != null && storages[1] != null

}

SK Panchal answered 2020-01-23T03:02:06Z

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐