Csharp
Csharp

Reputation: 177

Directory creation not working in Marshmallow Android

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

}

Upvotes: 2

Views: 5112

Answers (3)

Sohail Zahid
Sohail Zahid

Reputation: 8149

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;
            }
        }
    }

Upvotes: 12

Newbiee
Newbiee

Reputation: 592

Devices running on Android M, you cant directly write data to storage. you need to ask permission for that.

this link might be helpful

So the permission WRITE_EXTERNAL_STORAGE should be asked at run time here.

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;
}
}

You will also need to override onRequestPermissionsResult() callback method. and you can add the logic here.

@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. 
}

}

Hope it helps :)

Upvotes: 1

Shayan_Aryan
Shayan_Aryan

Reputation: 2042

In Android Marshmallow you need to ask for dangerous permissions, including STORAGE permission group, in runtime. I know a nice library that can make your life easier, RxPermissions.

You can ask for a permission with few code:

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

Upvotes: 0

Related Questions