youtube - How to get runtimepermission in Android 6.0 -
i using method youtubeintents.createuploadintent
, getting exception below,
java.lang.runtimeexception: unable start activity componentinfo{com.google.android.youtube/com.google.android.apps.youtube.app.honeycomb.shell$uploadactivity}: java.lang.securityexception: uid 10075 not have permission uri 0 @ content://media/external/video/media/7757
this exception means youtube application not have permission read_external_storage。
how handle exception?
you can use following class permission in runtime in marshmallow
import android.manifest; import android.app.activity; import android.content.pm.packagemanager; import android.support.v4.app.activitycompat; import android.support.v4.content.contextcompat; import android.widget.toast; public class marshmallowpermission { public static final int record_permission_request_code = 1; public static final int external_storage_permission_request_code = 2; public static final int camera_permission_request_code = 3; public static final int call_permission_request_code = 4; public static final int internet_permission_request_code=5; activity activity; public marshmallowpermission(activity activity) { this.activity = activity; } public boolean checkpermissionforexternalstorage(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.write_external_storage); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public boolean checkpermissionforcamera(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.camera); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public boolean checkpermissionforinternet(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.internet); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public boolean checkpermissionforcall(){ int result = contextcompat.checkselfpermission(activity, manifest.permission.call_phone); if (result == packagemanager.permission_granted){ return true; } else { return false; } } public void requestpermissionforexternalstorage(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.write_external_storage)){ toast.maketext(activity, "external storage permission needed. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.write_external_storage},external_storage_permission_request_code); } } public void requestpermissionforcamera(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.camera)){ toast.maketext(activity, "camera permission needed. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.camera},camera_permission_request_code); } } public void requestpermissionforcall(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.call_phone)){ toast.maketext(activity, "call permission needed. please allow in app settings additional functionality.", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.call_phone},call_permission_request_code); } } public void requestpermissionforinternet(){ if (activitycompat.shouldshowrequestpermissionrationale(activity, manifest.permission.internet)){ toast.maketext(activity, "camera permission needed. please allow in app settings additional functionality.internet", toast.length_long).show(); } else { activitycompat.requestpermissions(activity,new string[]{manifest.permission.internet},internet_permission_request_code); } } }
write following code in activity want permission forexternalstorage
if (!marshmallowpermission.checkpermissionforexternalstorage()) { marshmallowpermission.requestpermissionforexternalstorage(); } else { //to code here } @override public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) { switch (requestcode) { case marshmallowpermission.external_storage_permission_request_code: { // if request cancelled, result arrays empty. if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { // permission granted, yay! // contacts-related task need do. } else { // permission denied, boo! disable // functionality depends on permission. } return; } // other 'case' lines check other // permissions app might request } }