Android: Taking a picture using camera intent and saving it to a java.io.File -
Android: Taking a picture using camera intent and saving it to a java.io.File -
i'm trying image user , send our server. provide 2 options: choosing gallery or taking new image using default photographic camera in phones.
gallery thing works correctly, can photo selected , send without problem. in photographic camera option, photographic camera opens, takes picture, saves on sd card location specified, no problem, when returns application, nullpointerexception , application forced close. here do:
private string tempfilepath = ""; ... public void addphotousingcamera() { intent intent = new intent(mediastore.action_image_capture); tempfilepath = getoutputmediafilepath(media_type_image); intent.putextra(mediastore.extra_output, uri.fromfile(new file(tempfilepath))); startactivityforresult(intent, camera_activity_code); }
and utilize next photo taken:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { ... case camera_activity_code: if (resultcode == activity.result_ok) { image = new file(tempfilepath); } break; ... } }
i tried
bitmap thumbnail = (bitmap) data.getextras().get("data"); imageview image = (imageview) findviewbyid(r.id.phototaken); image.setimagebitmap(thumbnail);
inside case camera_activity_code:
no matter seek nullpointerexception in part of code.
there logcat info line 202 corresponds case camera_activity_code:
part.
01-31 23:40:13.154: error/androidruntime(8009): caused by: java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=101, result=-1, data=null} activity {com.ollaa.android/com.ollaa.android.share.sharefinalscreen}: java.lang.nullpointerexception 01-31 23:40:13.154: error/androidruntime(8009): caused by: java.lang.nullpointerexception 01-31 23:40:13.154: error/androidruntime(8009): @ com.ollaa.android.share.sharefinalscreen.onactivityresult(sharefinalscreen.java:202)
any help appreciated.
note: have declared necessary permissions in androidmanifest related photographic camera , sd card.
edit: realized tempfilepath not lastly until photographic camera activity returns app. set initialization tempfilepath in oncreate
method time image not set when seek reach outside function other onactivityresult
. sense not know java @ all, when alter class variable within function, changed value should visible functions of class right??
when using:
intent.putextra(mediastore.extra_output, myfile);
the file must exist on 'disk'. photographic camera activity not create it, imho bug. have utilize myfile.createnewfile();
also, have send photographic camera activity file , not path.
try this:
tempfilepath = getoutputmediafilepath(media_type_image); string filename = "myphoto"; file myfile = new file (tempfilepath,filename); myfile.createnewfile(); // @ point stop debugger , check if file exists on 'disk'. intent.putextra(mediastore.extra_output, uri.fromfile(myfile)));
there may improve way without using mediastore.extra_output
. maybe user can help.
also, note if user presses cancel , not take image have delete file.
note: using android api 2.2.
i hope helps.
android file nullpointerexception android-camera android-camera-intent
Comments
Post a Comment