目录创建无法在Marshmallow Android中运行 [英] Directory creation not working in Marshmallow Android

查看:143
本文介绍了目录创建无法在Marshmallow Android中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的原生应用程序中创建目录。它在Android 5.0中完美运行,但在Android 6.0(Marshmallow)中有时它工作,有时没有。我正在使用以下代码创建新目录。我正在保存视频和目录中的图像文件。

I am creating directory in my native application.It is working perfectly in Android 5.0, But In Android 6.0 (Marshmallow) some times it worked and some times not.I am using following code to create new directory.I am saving Video and image file in directory.

 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();

}


推荐答案

public class MyDevIDS extends AppCompatActivity {

        private static final int REQUEST_RUNTIME_PERMISSION = 123;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (CheckPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // you have permission go ahead
                createApplicationFolder();
            } else {
                // you do not have permission go request runtime permissions
                RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
            }
        }

        @Override
        public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

            switch (permsRequestCode) {

                case REQUEST_RUNTIME_PERMISSION: {
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        // you have permission go ahead
                        createApplicationFolder();
                    } else {
                        // you do not have permission show toast.
                    }
                    return;
                }
            }
        }

        public void RequestPermission(Activity thisActivity, String Permission, int Code) {
            if (ContextCompat.checkSelfPermission(thisActivity,
                    Permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        Permission)) {
                } else {
                    ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Permission},
                            Code);
                }
            }
        }

        public boolean CheckPermission(Context context, String Permission) {
            if (ContextCompat.checkSelfPermission(context,
                    Permission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }
    }

这篇关于目录创建无法在Marshmallow Android中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆