在可移动存储设备上创建文件夹

时间:2018-01-24 19:29:15

标签: android android-permissions

我需要在android外部可移动存储设备上创建一个文件夹kalid。我已经包含了清单读写权限。但我的文件夹是在手机存储上创建的。我已经实现了教程中的两个代码。

String storagestate = Environment.getExternalStorageState();

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

            String folder_main = "Kalids";
            File f = new File(Environment.getExternalStorageDirectory(), folder_main);


            if (!f.exists()) {
                f.mkdir();
            }
        };

上面的代码在内部存储器中创建了kalids文件夹。还有这段代码

String folder_main = "Kalid";

        File f = new File(Environment.getExternalStorageDirectory(), folder_main);
        if (!f.exists()) {
            f.mkdirs();
        }

我的清单如下所示

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如何在外部存储设备中创建文件夹?

1 个答案:

答案 0 :(得分:0)

boolean isAvialable= false;
        boolean isPermissable= false;
        String storagestate = Environment.getExternalStorageState();

        if (storagestate.equals(Environment.MEDIA_MOUNTED)) {
            isAvialable = true;
            isPermissable = true;

            String folder_main = "kalid";
            File f = new File(Environment.getExternalStorageDirectory(), folder_main);


            if (!f.exists()) {
                f.mkdir();
            }
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(storagestate)) {

            isAvialable = true;
            isPermissable = false;
        } else {

            isAvialable = isPermissable = false;
            Toast.makeText(MainActivity.this, "Read Only Media Can't Write" ,Toast.LENGTH_LONG).show();
        }

在sdcard中创建文件夹kalid。请不要忘记为marshmallow添加运行时权限。