android - capturing image and displaying -


i trying click images camera , displaying them in gridview. using below code getting "unfortunately camera has stopped working" error. please suggest

logcat:beginning of crash**07-09 01:14:01.734 3873-3873/? e/androidruntime﹕ fatal exception: main process: com.example.sakshi.intel, pid: 3873 java.lang.runtimeexception: unable start activity componentinfo{com.example.sakshi.intel/com.example.sakshi.intel.mainactivity}: java.lang.nullpointerexception: attempt invoke interface method 'int java.util.list.size()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ 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 interface method 'int java.util.list.size()' on null object reference @ com.example.sakshi.intel.mainactivity$imageadapter.getcount(mainactivity.java:89) @ android.widget.gridview.setadapter(gridview.java:201) @ com.example.sakshi.intel.mainactivity.oncreate(mainactivity.java:45) @ android.app.activity.performcreate(activity.java:5933) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ 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)**

using below code: activity_main.xml`

<button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="photo"     android:onclick="takepicture"/>  <gridview     android:id="@+id/gridview1"     android:layout_width="match_parent"     android:layout_height="wrap_content"></gridview> 

`

mainactivity.java 

public class mainactivity extends actionbaractivity {

private list<string> mylist;  // string list contains file paths images private gridview gridview; private string mcurrentphotopath;  // file path last image captured  static final int request_take_photo = 1;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // initialize gridview     gridview = (gridview) findviewbyid(r.id.gridview1);     gridview.setadapter(new imageadapter(this));      // initialize gridview thumbnail click handler         /*gridview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view v, int position, long id) {                 // send file path image display activity                 intent intent = new intent(getapplicationcontext(), imagedisplayactivity.class);                 intent.putextra("path", mylist.get(position));                 startactivity(intent);             }         });*/ }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  public class imageadapter extends baseadapter {     private context mcontext;      public imageadapter(context c) {         mcontext = c;     }      public int getcount() {         return mylist.size();     }      public object getitem(int position) {         return null;     }      public long getitemid(int position) {         return 0;     }      public view getview(int position, view convertview, viewgroup parent) {         imageview imageview;          if (convertview == null) {             imageview = new imageview(mcontext);             imageview.setlayoutparams(new gridview.layoutparams(85, 85));             imageview.setscaletype(imageview.scaletype.center_crop);             imageview.setpadding(8, 8, 8, 8);         } else {             imageview = (imageview) convertview;         }          bitmapfactory.options bmoptions = new bitmapfactory.options();         bmoptions.injustdecodebounds = false;         bmoptions.insamplesize = 4;         bmoptions.inpurgeable = true;         bitmap bitmap = bitmapfactory.decodefile(mylist.get(position), bmoptions);          imageview.setimagebitmap(bitmap);          return imageview;     } }   private void takepicture() {     intent takepictureintent = new intent(mediastore.action_image_capture );     if (takepictureintent.resolveactivity(getpackagemanager()) != null)     {         file photofile = null;         try         {             photofile = createimagefile();         }         catch (ioexception ex)         {             // error occurred while creating file         }         // continue if file created         if (photofile != null)         {             takepictureintent.putextra(mediastore. extra_output,                     uri. fromfile(photofile));             startactivityforresult(takepictureintent, request_take_photo);         }     } }  private file createimagefile() throws ioexception {     // create image file name     string timestamp = new simpledateformat("yyyymmdd_hhmmss").format( new date());     string imagefilename = "jpeg_" + timestamp + "_" ;     file storagedir = environment.getexternalstoragepublicdirectory(environment.directory_pictures);     file image = file. createtempfile(             imagefilename,  /* prefix */             ".jpg",         /* suffix */             storagedir      /* directory */     );      // save file: path use action_view intents     mcurrentphotopath = image.getabsolutepath();     return image; }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (requestcode == request_take_photo && resultcode == result_ok)     {         // save image gallery         intent mediascanintent = new intent(intent.action_media_scanner_scan_file );         file f = new file(mcurrentphotopath );         uri contenturi = uri.fromfile(f);         mediascanintent.setdata(contenturi);         this.sendbroadcast(mediascanintent);          // add image path list         mylist.add( mcurrentphotopath);          // refresh gridview image thumbnails         gridview.invalidateviews();     } } 

}

you trying access size of list, mylist in getcount(), inside imageadapter class. , exception happened.

the exception occurred because mylist null object.

why null? possibly because in mylist.add( mcurrentphotopath); inside onactivityresult, value of mcurrentphotopath might null.

add check inside onactivityresult :

if (mcurrentphotopath != null) { mylist.add(mcurrentphotopath); } else { // can't access image file path.show toast or something. }

now, experience phone manufactures return file path, return bitmap of image captured. not sure, might return both. expecting access file path of captured image won't app.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -