error capture photo and display thumbnail android studio -


hi i'm making app internship , got error.
please me solve this

this activity.java

private void dispatchtakepictureintent() {     intent takepictureintent = new intent(mediastore.action_image_capture);     // ensure there's camera activity handle intents     if (takepictureintent.resolveactivity(getpackagemanager()) != null) {         // create file photo should go         file photofile = null;         try {             photofile = createimagefile();         } catch (ioexception ex) {             // error occurred while creating file         }         // continue if file created         if (photofile != null) {             uri photouri = fileprovider.geturiforfile(this, "com.example.android.provider", photofile);             takepictureintent.putextra(mediastore.extra_output, photouri);             startactivityforresult(takepictureintent, request_take_photo);         }     } } 

and onactivityresult() code:

@override protected void onactivityresult(int requestcode, int resultcode, intent data){     super.onactivityresult(requestcode, resultcode, data);     if (resultcode == result_ok && requestcode == request_take_photo) {         bundle extras = data.getextras();         bitmap imagebitmap = (bitmap) extras.get("data");         imageview.setimagebitmap(imagebitmap);     }     if (resultcode == activity.result_ok){         if (requestcode == pick_file_request){             if (data == null){                 return;             }              uri selectedfileuri = data.getdata();             selectedfilepath = filepath.getpath(this, selectedfileuri);             log.i(tag,"selected file path:"+selectedfilepath);              if (selectedfilepath != null && !selectedfilepath.equals("")){                 tv_file_name.settext(selectedfilepath);             }else{                 toast.maketext(this, "cannot upload file server", toast.length_short).show();             }         }     } } 

and got this

java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=-1, data=intent {  }} activity {com.example.febryan.apptest/com.example.febryan.apptest.requestactivity2}: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.object android.os.bundle.get(java.lang.string)' on null object reference                                                                            @ android.app.activitythread.deliverresults(activitythread.java:4324)                                                                            @ android.app.activitythread.handlesendresult(activitythread.java:4367)                                                                            @ android.app.activitythread.-wrap19(unknown source:0)                                                                            @ android.app.activitythread$h.handlemessage(activitythread.java:1649)                                                                            @ android.os.handler.dispatchmessage(handler.java:105)                                                                            @ android.os.looper.loop(looper.java:164)                                                                            @ android.app.activitythread.main(activitythread.java:6541)                                                                            @ java.lang.reflect.method.invoke(native method)                                                                            @ com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240)                                                                            @ com.android.internal.os.zygoteinit.main(zygoteinit.java:767)                                                                         caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.object android.os.bundle.get(java.lang.string)' on null object reference                                                                            @ com.example.febryan.apptest.requestactivity2.onactivityresult(requestactivity2.java:311)                                                                            @ android.app.activity.dispatchactivityresult(activity.java:7235)                                                                            @ android.app.activitythread.deliverresults(activitythread.java:4320) 

please me solve this. thx

the default android camera application returns non-null intent when passing thumbnail in returned intent. if pass extra_output uri write to, return null intent , picture in uri passed in.

so since passing extra_output location intent retun null data , i.e in case photo / captured image @ photouri path.

so instead of looking data intent in onactivityresult() take photouri path. below.here photo file file extra_output location saved actual camera image

@override protected void onactivityresult(int requestcode, int resultcode, intent data){     super.onactivityresult(requestcode, resultcode, data);     if (resultcode == result_ok && requestcode == request_take_photo) {          uri uri = uri.fromfile(photofile);  // here photo file file extra_output location saved actual camera image         bitmap bitmap;         try {             bitmap = mediastore.images.media.getbitmap(getcontentresolver(), uri);             bitmap = crupandscale(bitmap, 300); // if mind scaling             pofileimageview.setimagebitmap(bitmap);         } catch (filenotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          imageview.setimagebitmap(bitmap);     }  } 

if crop , scale image , mention thumbnail image.

public static  bitmap crupandscale (bitmap source,int scale){     int factor = source.getheight() <= source.getwidth() ? source.getheight(): source.getwidth();     int longer = source.getheight() >= source.getwidth() ? source.getheight(): source.getwidth();     int x = source.getheight() >= source.getwidth() ?0:(longer-factor)/2;     int y = source.getheight() <= source.getwidth() ?0:(longer-factor)/2;     source = bitmap.createbitmap(source, x, y, factor, factor);     source = bitmap.createscaledbitmap(source, scale, scale, false);     return source; } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -