android - null pointer exception - image capture -
i'm beginner. didn't know messed up. appreciated. 2nd activity has button intent open 3rd activity. 3rd activity should start open camera function , wait image captured before being displayed. 3rd activity failed started. here code.
java code(2nd activity).
public class cameraview extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_camera_view); // show button in action bar. } public void onclickcam(view view) { intent intent = new intent(this, resultactivity.class); startactivity(intent); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); } }
java code(3rd activity).
public class resultactivity extends activity { private static final int action_take_photo_b = 1; private static final string bitmap_storage_key = "viewbitmap"; private static final string imageview_visibility_storage_key = "imageviewvisibility"; private imageview mimageview; private bitmap mimagebitmap; private string mcurrentphotopath; private static final string jpeg_file_prefix = "img_"; private static final string jpeg_file_suffix = ".jpg"; private albumstoragedirfactory malbumstoragedirfactory = null; /* photo album application */ private string getalbumname() { return getstring(r.string.album_name); } private file getalbumdir() { file storagedir = null; if (environment.media_mounted.equals(environment.getexternalstoragestate())) { storagedir = malbumstoragedirfactory.getalbumstoragedir(getalbumname()); if (storagedir != null) { if (! storagedir.mkdirs()) { if (! storagedir.exists()){ log.d("camerasample", "failed create directory"); return null; } } } } else { log.v(getstring(r.string.app_name), "external storage not mounted read/write."); } return storagedir; } private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); string imagefilename = jpeg_file_prefix + timestamp + "_"; file albumf = getalbumdir(); file imagef = file.createtempfile(imagefilename, jpeg_file_suffix, albumf); return imagef; } private file setupphotofile() throws ioexception { file f = createimagefile(); mcurrentphotopath = f.getabsolutepath(); return f; } private void setpic() { /* there isn't enough memory open more couple camera photos */ /* pre-scale target bitmap file decoded */ /* size of imageview */ int targetw = mimageview.getwidth(); int targeth = mimageview.getheight(); /* size of image */ bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodefile(mcurrentphotopath, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; /* figure out way needs reduced less */ int scalefactor = 1; if ((targetw > 0) || (targeth > 0)) { scalefactor = math.min(photow/targetw, photoh/targeth); } /* set bitmap options scale image decode target */ bmoptions.injustdecodebounds = false; bmoptions.insamplesize = scalefactor; bmoptions.inpurgeable = true; /* decode jpeg file bitmap */ bitmap bitmap = bitmapfactory.decodefile(mcurrentphotopath, bmoptions); /* associate bitmap imageview */ mimageview.setimagebitmap(bitmap); mimageview.setvisibility(view.visible); } private void galleryaddpic() { intent mediascanintent = new intent("android.intent.action.media_scanner_scan_file"); file f = new file(mcurrentphotopath); uri contenturi = uri.fromfile(f); mediascanintent.setdata(contenturi); this.sendbroadcast(mediascanintent); } private void dispatchtakepictureintent(int actioncode) { intent takepictureintent = new intent(mediastore.action_image_capture); switch(actioncode) { case action_take_photo_b: file f = null; try { f = setupphotofile(); mcurrentphotopath = f.getabsolutepath(); takepictureintent.putextra(mediastore.extra_output, uri.fromfile(f)); } catch (ioexception e) { e.printstacktrace(); f = null; mcurrentphotopath = null; } break; default: break; } // switch startactivityforresult(takepictureintent, actioncode); } private void handlebigcameraphoto() { if (mcurrentphotopath != null) { setpic(); galleryaddpic(); mcurrentphotopath = null; } } /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_result); mimageview = (imageview) findviewbyid(r.id.imageview1); mimagebitmap = null; intent intent = new intent(this, resultactivity.class); dispatchtakepictureintent(action_take_photo_b); startactivity(intent); if (build.version.sdk_int >= build.version_codes.froyo) { malbumstoragedirfactory = new froyoalbumdirfactory(); } else { malbumstoragedirfactory = new basealbumdirfactory(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case action_take_photo_b: { if (resultcode == result_ok) { handlebigcameraphoto(); } break; } // action_take_photo_b } // switch } // lifecycle callbacks image can survive orientation change @override protected void onsaveinstancestate(bundle outstate) { outstate.putparcelable(bitmap_storage_key, mimagebitmap); outstate.putboolean(imageview_visibility_storage_key, (mimagebitmap != null) ); super.onsaveinstancestate(outstate); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); mimagebitmap = savedinstancestate.getparcelable(bitmap_storage_key); mimageview.setimagebitmap(mimagebitmap); mimageview.setvisibility( savedinstancestate.getboolean(imageview_visibility_storage_key) ? imageview.visible : imageview.invisible ); } /** * indicates whether specified action can used intent. * method queries package manager installed packages can * respond intent specified action. if no suitable package * found, method returns false. * http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html * * @param context application's environment. * @param action intent action check availability. * * @return true if intent specified action can sent , * responded to, false otherwise. */ public static boolean isintentavailable(context context, string action) { final packagemanager packagemanager = context.getpackagemanager(); final intent intent = new intent(action); list<resolveinfo> list = packagemanager.queryintentactivities(intent, packagemanager.match_default_only); return list.size() > 0; } }
xml code.
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".resultactivity" > <imageview android:id="@+id/imageview1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:visibility="visible" android:layout_centerhorizontal="true" android:layout_centervertical="true" />
logcat
08-26 01:45:55.845: e/androidruntime(3471): fatal exception: main 08-26 01:45:55.845: e/androidruntime(3471): java.lang.runtimeexception: unable start activity componentinfo{com.example.camoid/com.example.camoid.resultactivity}: java.lang.nullpointerexception 08-26 01:45:55.845: e/androidruntime(3471): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 08-26 01:45:55.845: e/androidruntime(3471): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 08-26 01:45:55.845: e/androidruntime(3471): @ android.app.activitythread.access$600(activitythread.java:141) 08-26 01:45:55.845: e/androidruntime(3471): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 08-26 01:45:55.845: e/androidruntime(3471): @ android.os.handler.dispatchmessage(handler.java:99) 08-26 01:45:55.845: e/androidruntime(3471): @ android.os.looper.loop(looper.java:137) 08-26 01:45:55.845: e/androidruntime(3471): @ android.app.activitythread.main(activitythread.java:5103) 08-26 01:45:55.845: e/androidruntime(3471): @ java.lang.reflect.method.invokenative(native method) 08-26 01:45:55.845: e/androidruntime(3471): @ java.lang.reflect.method.invoke(method.java:525) 08-26 01:45:55.845: e/androidruntime(3471): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-26 01:45:55.845: e/androidruntime(3471): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-26 01:45:55.845: e/androidruntime(3471): @ dalvik.system.nativestart.main(native method) 08-26 01:45:55.845: e/androidruntime(3471): caused by: java.lang.nullpointerexception 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.getalbumdir(resultactivity.java:47) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.createimagefile(resultactivity.java:69) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.setupphotofile(resultactivity.java:76) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.dispatchtakepictureintent(resultactivity.java:135) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.oncreate(resultactivity.java:171) 08-26 02:33:57.415: e/androidruntime(3730): @ android.app.activity.performcreate(activity.java:5133) 08-26 02:33:57.415: e/androidruntime(3730): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-26 02:33:57.415: e/androidruntime(3730): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175)
dispatchtakepictureintent(action_take_photo_b); startactivity(intent); if (build.version.sdk_int >= build.version_codes.froyo) { malbumstoragedirfactory = new froyoalbumdirfactory(); } else { malbumstoragedirfactory = new basealbumdirfactory(); }
here dispatchtakepictureintent()
indirectly calls function getalbumdir()
that uses malbumstoragedirfactory
not initialized yet, observed in logcat:
08-26 01:45:55.845: e/androidruntime(3471): caused by: java.lang.nullpointerexception 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.getalbumdir(resultactivity.java:47) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.createimagefile(resultactivity.java:69) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.setupphotofile(resultactivity.java:76) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.dispatchtakepictureintent(resultactivity.java:135) 08-26 02:33:57.415: e/androidruntime(3730): @ com.example.camoid.resultactivity.oncreate(resultactivity.java:171)
set malbumstoragedirfactory
before calling dispatchtakepictureintnet()
.
Comments
Post a Comment