android - Capture with camera and Crop the Image Capture -
i learning cropping image. follow tutorial.
i follow tutorial throughly, , run code in android studio, when click button, force closed. please me, here code follow website.
import android.content.activitynotfoundexception; import android.content.intent; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.toast; public class mainactivity extends appcompatactivity implements view.onclicklistener { final int pic_crop = 2; final int camera_capture = 1; private uri picuri; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //retrieve reference ui button button capturebtn = (button) findviewbyid(r.id.capture_btn); //handle button clicks capturebtn.setonclicklistener(this); } public void onclick(view v) { if (v.getid() == r.id.capture_btn) { try { //use standard intent capture image intent captureintent = new intent(mediastore.action_image_capture); //we handle returned data in onactivityresult startactivityforresult(captureintent, camera_capture); } catch (activitynotfoundexception anfe) { //display error message string errormessage = "whoops - device doesn't support capturing images!"; toast toast = toast.maketext(this, errormessage, toast.length_short); toast.show(); } } } protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { if (requestcode == camera_capture) { picuri = data.getdata(); performcrop(); } else if (requestcode == pic_crop) { //get returned data bundle extras = data.getextras(); //get cropped bitmap bitmap thepic = extras.getparcelable("data"); //retrieve reference imageview imageview picview = (imageview) findviewbyid(r.id.picture); //display returned cropped image picview.setimagebitmap(thepic); } } } private void performcrop() { try { intent cropintent = new intent("com.android.camera.action.crop"); cropintent.setdataandtype(picuri, "image/*"); cropintent.putextra("crop", "true"); cropintent.putextra("aspectx", 1); cropintent.putextra("aspecty", 1); cropintent.putextra("outputx", 256); cropintent.putextra("outputd", 256); cropintent.putextra("return-data", true); startactivityforresult(cropintent, pic_crop); } catch (activitynotfoundexception anfe) { string errormessage = "whoops"; toast toast = toast.maketext(this, errormessage, toast.length_short); toast.show(); } } }
this logcat
05-09 18:26:49.013 2323-2323/com.don.cropping e/androidruntime: fatal exception: main process: com.don.cropping, pid: 2323 java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=0, data=null} activity {com.don.cropping/com.don.cropping.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.net.uri android.content.intent.getdata()' on null object reference @ android.app.activitythread.deliverresults(activitythread.java:3539) @ android.app.activitythread.handlesendresult(activitythread.java:3582) @ android.app.activitythread.access$1300(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1327) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.net.uri android.content.intent.getdata()' on null object reference @ com.don.cropping.mainactivity.onactivityresult(mainactivity.java:56) @ android.app.activity.dispatchactivityresult(activity.java:6139) @ android.app.activitythread.deliverresults(activitythread.java:3535) @ android.app.activitythread.handlesendresult(activitythread.java:3582) @ android.app.activitythread.access$1300(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1327) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
activity.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.don.cropping.mainactivity"> <textview android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> <button android:id="@+id/capture_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/capture" /> <imageview android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/capture_btn" android:layout_margin="5dp" android:background="@drawable/pic_border" android:contentdescription="@string/picture" /> </linearlayout>
Comments
Post a Comment