android - EACCES (Permission denied) after granting the necessary permissions -
okay having problem while testing app on emulator (android 6.0) (since dont have android 6.0 device ) @ first asking user grant necessary permission before executing task , after getting permission executing task giving me error :
unable decode stream: java.io.filenotfoundexception: /storage/19fa-1f0b/dcim/image1464800718267.jpg: open failed: eacces (permission denied) 06-02 12:04:11.906 21595-21595/pb.mypackage d/androidruntime: shutting down vm ----------beginning of crash 06-02 12:04:11.907 21595-21595/pb.mypackage e/androidruntime: fatal exception: main process: pb.mypackage, pid: 21595 java.lang.runtimeexception: unable start activity componentinfo{pb.mypackage/pb.mypackage.cropactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.graphics.bitmap$config android.graphics.bitmap.getconfig()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.graphics.bitmap$config android.graphics.bitmap.getconfig()' on null object reference
my whole code asking , performing task :
if (build.version.sdk_int >= 23){ if (contextcompat.checkselfpermission(context, manifest.permission.read_external_storage) != packagemanager.permission_granted) { requestpermissions(new string[] {manifest.permission.read_external_storage, manifest.permission.write_external_storage}, request_code_ask_permissions); }else { //start activity select image gallery intent gallery_intent = new intent(getcontext(), galleryutil.class); gallery_intent.putextra("switch", 0); startactivityforresult(gallery_intent, gallery_activity_code); } }else { //start activity select image gallery intent gallery_intent = new intent(getcontext(), galleryutil.class); gallery_intent.putextra("switch", 0); startactivityforresult(gallery_intent, gallery_activity_code); }
as can see checking if necessary permissions granted or not still getting exceptions , 1 important thing happening when first time performing of above task , @ first user granted permission getting error when run app again not getting exceptions , working fine
seems when user granting permission new change haven't yet updated on system , after restart changes updates done , gets fine
onactivityresult: @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == gallery_activity_code) { if(resultcode == activity.result_ok) { string picturepath = data.getstringextra("picturepath"); //perform crop on image selected gallery performcrop(picturepath);}} if (requestcode == result_crop ) { if(resultcode == activity.result_ok){ bundle extras = data.getextras(); bitmap selectedbitmap = extras.getparcelable("data"); // set bitmap data imageview imageview.setimagebitmap(selectedbitmap); imageview.setscaletype(imageview.scaletype.fit_xy);}} } private void performcrop(string imageuri) { intent cropintent = new intent(getactivity(), cropactivity.class ); cropintent.putextra("imageuri", imageuri); startactivity(cropintent); getactivity().finish(); }
here after getting image sending cropping , there getting image result , , error occurring in cropactivity.class
galleryclass:
public class galleryutil extends activity{ private final static int result_select_image = 100; final private int request_code_ask_permissions = 123; public static final int media_type_image = 1; private static final string tag = "galleryutil"; string mcurrentphotopath; file photofile = null; intent selectedimageintent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try{ //pick image gallery intent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); startactivityforresult(i, result_select_image); }catch(exception e){ e.printstacktrace(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch(requestcode){ case result_select_image: if (resultcode == activity.result_ok && data != null && data.getdata() != null) { try{ uri selectedimage = data.getdata(); string[] filepathcolumn = {mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); //return image path main activity intent returnfromgalleryintent = new intent(); returnfromgalleryintent.putextra("switch", false); returnfromgalleryintent.putextra("picturepath",picturepath); setresult(result_ok,returnfromgalleryintent); finish(); }catch(exception e){ e.printstacktrace(); intent returnfromgalleryintent = new intent(); setresult(result_canceled, returnfromgalleryintent); finish(); } }else{ log.i(tag, "result_canceled"); intent returnfromgalleryintent = new intent(); setresult(result_canceled, returnfromgalleryintent); finish(); } break; } }
cropactivity :
public class cropactivity extends activity { file savedfile; button btncancel; context context; photoviewattacher mattacher; bitmap mimage; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.new_crop_actvity); final cropimageview cropimageview = (cropimageview)findviewbyid(r.id.cropimageview); final imageview croppedimageview = (imageview)findviewbyid(r.id.croppedimageview); final imageview bgimgview = (imageview) findviewbyid(r.id.blurbgimgview); btncancel = (button)findviewbyid(r.id.btncancel); context = this.getbasecontext(); mattacher = new photoviewattacher(croppedimageview); uri imguri1 = uri.parse(getintent().getextras().getstring("imageuri")); string uristr = imguri1.tostring(); file f = new file(uristr); final string filepath = f.getpath(); bitmap bitmap = bitmapfactory.decodefile(filepath); cropimageview.setimagebitmap(bitmap); bitmap blurredbitmap = blur.fastblur(this, bitmap, 20); bgimgview.setimagebitmap(blurredbitmap); cropimageview.setcropmode(cropimageview.cropmode.circle); //customising crop activity cropimageview.setbackgroundcolor(getresources().getcolor(r.color.transparent)); cropimageview.setoverlaycolor(0xaa1c1c1c); cropimageview.setframecolor(getresources().getcolor(r.color.transparent)); cropimageview.sethandlecolor(getresources().getcolor(r.color.transparent)); cropimageview.setguidecolor(getresources().getcolor(r.color.transparent)); // set image cropping //cropimageview.setimagebitmap(bitmapfactory.decoderesource(getresources(), r.mipmap.ic_launcher)); //cropimageview.setimagebitmap(mimage); button cropbutton = (button)findviewbyid(r.id.crop_button); cropbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // bitmap croppediamge = cropimageview.getcroppedbitmap(); // cropped image, , show result. croppedimageview.setimagebitmap(cropimageview.getcroppedbitmap()); view content = croppedimageview; content.setdrawingcacheenabled(true); bitmap bitmap = content.getdrawingcache(); // // file root = environment.getexternalstoragedirectory(); // file file = new file(root.getabsolutepath() + "/download/img.jpg"); // file root = environment.getexternalstoragedirectory(); file file = new file(context.getapplicationcontext().getfilesdir() + "/img.gpj"); string path = file.getpath(); // file file = new file("/dcim/camera/image.jpg"); try { file.createnewfile(); fileoutputstream ostream = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 60, ostream); savedfile = new file(path); ostream.close(); } catch (exception e) { e.printstacktrace(); } croppedimageview.builddrawingcache(); bitmap croppediamge = croppedimageview.getdrawingcache(); intent sendcroppedimgintent = new intent(cropactivity.this, mainactivity.class); bundle extras = new bundle(); //extras.putparcelable("imagebitmap", croppediamge); extras.putstring("croppedimguri", savedfile.tostring() ); extras.putint("switch",1); sendcroppedimgintent.putextras(extras); startactivity(sendcroppedimgintent); } }); } public void cancel(view view) { intent cancelintent = new intent(cropactivity.this, mainactivity.class); startactivity(cancelintent); } }
if (build.version.sdk_int >= 23) { if (contextcompat.checkselfpermission(activityname.this, manifest.permission.read_external_storage) != packagemanager.permission_granted) { requestpermissions(new string[]{manifest.permission.read_external_storage, manifest.permission.write_external_storage}, request_code_ask_permissions); } else { intent gallery_intent = new intent(getcontext(), galleryutil.class); gallery_intent.putextra("switch", 0); startactivityforresult(gallery_intent, gallery_activity_code); } } else { //start activity select image gallery intent gallery_intent = new intent(getcontext(), galleryutil.class); gallery_intent.putextra("switch", 0); startactivityforresult(gallery_intent, gallery_activity_code); } @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { switch (requestcode) { case request_code_ask_permissions: final int numofrequest = grantresults.length; final boolean isgranted = numofrequest == 1 && packagemanager.permission_granted == grantresults[numofrequest - 1]; if (isgranted) { intent gallery_intent = new intent(getcontext(), galleryutil.class); gallery_intent.putextra("switch", 0); startactivityforresult(gallery_intent, gallery_activity_code); } break; default: super.onrequestpermissionsresult(requestcode, permissions, grantresults); } }