Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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
Java 目录创建在Marshmallow Android中不起作用_Java_Android_Android 6.0 Marshmallow - Fatal编程技术网

Java 目录创建在Marshmallow Android中不起作用

Java 目录创建在Marshmallow Android中不起作用,java,android,android-6.0-marshmallow,Java,Android,Android 6.0 Marshmallow,我正在我的原生应用程序中创建目录。它在Android 5.0中运行得很好,但在Android 6.0(棉花糖)中,有时运行得很好,有时不运行。我正在使用以下代码创建新目录。我正在目录中保存视频和图像文件 public static void createApplicationFolder() { File f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPR

我正在我的原生应用程序中创建目录。它在Android 5.0中运行得很好,但在Android 6.0(棉花糖)中,有时运行得很好,有时不运行。我正在使用以下代码创建新目录。我正在目录中保存视频和图像文件

 public static void createApplicationFolder() {
    File f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME);
    f.mkdirs();
    f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_COMPRESSED_VIDEOS_DIR);
    f.mkdirs();
    f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_TEMP_DIR);
    f.mkdirs();

}

在安卓M上运行的设备,您不能直接将数据写入存储器。你需要得到允许

链接可能会有所帮助

因此,在运行时应在此处询问权限
写入外部存储

public  boolean permissions() {
//cheeck id device is Android M 

 if (Build.VERSION.SDK_INT >= 23) {
    if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        //your permission is granted
        return true;
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else {
 //permission is automatically granted on devices lower than android M upon installation

    return true;
}
}
您还需要重写onRequestPermissionsResult()回调方法。你可以在这里添加逻辑

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){

    //Add your logic of saving a file to directory. 
}

}

希望它能有所帮助:)

在Android棉花糖中,您需要在运行时请求,包括存储权限组。 我知道一个很好的图书馆可以让你的生活更轻松

您可以使用少量代码请求权限:

RxPermissions.getInstance(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
    if (granted) { // Always true pre-M
       // I can write to the storage now
    } else {
       // Oups permission denied
    }
});

在android M中,你需要一个请求权限。我已经在清单中添加了权限,还尝试了运行时权限。但它不起作用。还有其他方法吗?是的,你应该请求引用@Csharp查看我编辑的答案。请从引用的链接添加相关代码。将来,此链接可能会更改,从而使答案对未来的访问者无效。它是否也适用于READ_EXTERNAL_STORAGE?@Csharp不客气,如果有帮助,请勾选。
RxPermissions.getInstance(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
    if (granted) { // Always true pre-M
       // I can write to the storage now
    } else {
       // Oups permission denied
    }
});