App crash on android seven -
when run app on android 7 app crashes. android monitor throws :
caused by: android.os.fileuriexposedexception: file:///storage/emulated/0/picture.jpg exposed beyond app through clipdata.item.geturi()
and code is
@override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); switch (requestcode) { case request_write_permission: if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { takepicture(); } else { toast.maketext(mainactivity.this, "permission denied!", toast.length_short).show(); } } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == photo_request && resultcode == result_ok) { toast.maketext(this,"click on text save data",toast.length_long).show(); launchmediascanintent(); try { bitmap bitmap = decodebitmapuri(this, imageuri); if (detector.isoperational() && bitmap != null) { frame frame = new frame.builder().setbitmap(bitmap).build(); sparsearray<textblock> textblocks = detector.detect(frame); string blocks = ""; string lines = ""; string words = ""; (int index = 0; index < textblocks.size(); index++) { //extract scanned text blocks here textblock tblock = textblocks.valueat(index); blocks = blocks + tblock.getvalue() + "\n" + "\n"; (text line : tblock.getcomponents()) { //extract scanned text lines here lines = lines + line.getvalue() + "\n"; (text element : line.getcomponents()) { //extract scanned text words here words = words + element.getvalue() + ", "; } } } if (textblocks.size() == 0) { scanresults.settext("scan failed: found nothing scan"); } else { scanresults.settext(scanresults.gettext() + "\n"); scanresults.settext(scanresults.gettext() + blocks + "\n"); } } else { scanresults.settext("could not set detector!"); } } catch (exception e) { toast.maketext(this, "failed load image", toast.length_short) .show(); log.e(log_tag, e.tostring()); } } } private void takepicture() { intent intent = new intent(mediastore.action_image_capture); file photo = new file(environment.getexternalstoragedirectory(), "picture.jpg"); imageuri = uri.fromfile(photo); intent.putextra(mediastore.extra_output, imageuri); startactivityforresult(intent, photo_request); scanresults.settext(""); } @override protected void onsaveinstancestate(bundle outstate) { if (imageuri != null) { outstate.putstring(saved_instance_uri, imageuri.tostring()); outstate.putstring(saved_instance_result, scanresults.gettext().tostring()); } super.onsaveinstancestate(outstate); } private void launchmediascanintent() { intent mediascanintent = new intent(intent.action_media_scanner_scan_file); mediascanintent.setdata(imageuri); this.sendbroadcast(mediascanintent); } private bitmap decodebitmapuri(context ctx, uri uri) throws filenotfoundexception { int targetw = 600; int targeth = 600; bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodestream(ctx.getcontentresolver().openinputstream(uri), null, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; int scalefactor = math.min(photow / targetw, photoh / targeth); bmoptions.injustdecodebounds = false; bmoptions.insamplesize = scalefactor; return bitmapfactory.decodestream(ctx.getcontentresolver() .openinputstream(uri), null, bmoptions); } public void store_in_database(view view) { intent = new intent(mainactivity.this , dbactivity.class); i.putextra("message",scanresults.gettext().tostring()); mainactivity.this.startactivity(i); }
any in form of code great. understand may due file system permissions related android 7 . explanation in form of code welcomed. trying access camera , store image android database.
add code in mainactivity.java in oncreate()
if (contextcompat.checkselfpermission(googlemapsactivity.this, android.manifest.permission.read_external_storage) != packagemanager.permission_granted) { // should show explanation? if activitycompat.shouldshowrequestpermissionrationale(googlemapsactivity.this, android.manifest.permission.read_external_storage)) { } else { activitycompat.requestpermissions(googlemapsactivity.this, new string[]{android.manifest.permission.read_external_storage}, my_permissions_request_read_contacts); } }
Comments
Post a Comment