安卓系统5.1格式化sd卡的方法

public StorageVolume getVolume() {
        Method mMethodGetPaths;
        StorageManager mStorageManager;
        StorageVolume[] storageVolumes = null;
        try {
            mStorageManager = (StorageManager) mContext
                    .getSystemService(Activity.STORAGE_SERVICE);
            mMethodGetPaths = mStorageManager.getClass().getMethod(
                    "getVolumeList");
            storageVolumes = (StorageVolume[]) mMethodGetPaths
                    .invoke(mStorageManager);

            for (StorageVolume volume : storageVolumes) {
                Log.e("format", volume.getPath());
                Log.e("format----------",
                        volume.getPath().equals("/storage/sdcard1") + "");
                if (volume.getPath().equals("/storage/sdcard1")) {
                    Log.e("format", volume.getPath());
                    return volume;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

private void formatSD() { // format SDcard

        Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
        intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
        final StorageVolume storageVolume = getVolume();
        intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, storageVolume);
        startService(intent);

    }

以上的方法都需要安卓系统FrameWork的Jar包或者在服务器环境下编译

猜你喜欢

转载自blog.csdn.net/lwz622/article/details/80658549