Camera crashes after taking picture and hitting ok in Android Marshmallow.? -
i new android. creating imageupload activity in android app working fine under apk(23). whenever try on android marshmallow crashes after clicking picture.
here manifest file
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.android.permission.camera" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage"/> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.change_wifi_multicast_state" /> <application android:name=".parseapplication" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".image"></activity> <activity android:name=".main"></activity> <activity android:name=".singleitemview"></activity> </application>
here main activity
imageview viewimage; button b; button bt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.upload); b=(button)findviewbyid(r.id.btnselectphoto); bt=(button)findviewbyid(r.id.uploadbtn); viewimage=(imageview)findviewbyid(r.id.viewimage); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { selectimage(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds options action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } private void selectimage() { final charsequence[] options = { "take photo", "choose gallery","cancel" }; alertdialog.builder builder = new alertdialog.builder(image.this); builder.settitle("add photo!"); builder.setitems(options, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int item) { if (options[item].equals("take photo")) { intent intent = new intent(mediastore.action_image_capture); file f = new file(android.os.environment.getexternalstoragedirectory(), "temp.jpg"); intent.putextra(mediastore.extra_output, uri.fromfile(f)); startactivityforresult(intent, 1); } else if (options[item].equals("choose gallery")) { intent intent = new intent(intent.action_pick,android.provider.mediastore.images.media.external_content_uri); startactivityforresult(intent, 2); } else if (options[item].equals("cancel")) { dialog.dismiss(); } } }); builder.show(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == 1) { file f = new file(environment.getexternalstoragedirectory().tostring()); (file temp : f.listfiles()) { if (temp.getname().equals("temp.jpg")) { f = temp; break; } } try { bitmap bitmap; bitmapfactory.options bitmapoptions = new bitmapfactory.options(); bitmap = bitmapfactory.decodefile(f.getabsolutepath(), bitmapoptions); viewimage.setimagebitmap(bitmap); string path = android.os.environment .getexternalstoragedirectory() + file.separator + "phoenix" + file.separator + "default"; f.delete(); outputstream outfile = null; file file = new file(path, string.valueof(system.currenttimemillis()) + ".jpg"); try { outfile = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 85, outfile); outfile.flush(); outfile.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } else if (requestcode == 2) { uri selectedimage = data.getdata(); string[] filepath = {mediastore.images.media.data}; cursor c = getcontentresolver().query(selectedimage, filepath, null, null, null); c.movetofirst(); int columnindex = c.getcolumnindex(filepath[0]); string picturepath = c.getstring(columnindex); c.close(); bitmap thumbnail = (bitmapfactory.decodefile(picturepath)); log.w("path of image ", picturepath + ""); viewimage.setimagebitmap(thumbnail); } } bt.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { bt.setonclicklistener(this); // locate image in res > drawable-hdpi //bitmap bitmap = bitmapfactory.decoderesource(getresources(), // r.id.viewimage); bitmap bitmap = ((bitmapdrawable)viewimage.getdrawable()).getbitmap(); // convert byte bytearrayoutputstream stream = new bytearrayoutputstream(); // compress image lower quality scale 1 - 100 bitmap.compress(bitmap.compressformat.jpeg, 50, stream); byte[] image = stream.tobytearray(); // create parsefile parsefile file = new parsefile("temp.jpg", image); // upload image parse cloud file.saveinbackground(); // create new class called "imageupload" in parse parseobject imgupload = new parseobject("imageupload"); // create column named "imagename" , set string imgupload.put("imagename", "android"); // create column named "imagefile" , insert image imgupload.put("imagefile", file); // create class , columns imgupload.saveinbackground(); // show simple toast message toast.maketext(image.this, "image uploaded", toast.length_short).show(); intent = new intent(getapplicationcontext(), main.class); startactivity(i); } }); }}
here log
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=-1, data=intent { }} activity {com.example.user.mnc/com.example.user.mnc.image}: java.lang.nullpointerexception: attempt length of null array @ android.app.activitythread.deliverresults(activitythread.java:3699) @ android.app.activitythread.handlesendresult(activitythread.java:3742) @ android.app.activitythread.-wrap16(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1393) @ 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 length of null array @ com.example.user.mnc.image.onactivityresult(image.java:99)
you have ask permissions on runtime on android m.
more info @ http://developer.android.com/intl/es/training/permissions/requesting.html
Comments
Post a Comment