这是我的代码:
//Using intent to open camera Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,CAMERA_CAPTURE);
在activityResult中:
protected voID onActivityResult(int requestCode,int resultCode,Intent data){ if(resultCode==RESulT_OK) { if(requestCode==CAMERA_CAPTURE) { Bitmap pictTaken = null ; Bundle extras = data.getExtras(); if(extras.keySet().contains("data")) { pictTaken = (Bitmap) extras.get("data"); picUri = getIntent().getData(); } else{ picUri = getIntent().getData(); try { pictTaken = decodeUri(picUri); } catch (fileNotFoundException e) { // Todo auto-generated catch block e.printstacktrace(); } } Intent cropIntent= new Intent (this,Crop.class); cropIntent.putExtra("data",picUri.toString()); cropIntent.putExtra("pict",pictTaken); cropIntent.putExtra("code","camera"); startActivity(cropIntent); } }
捕获并保存它后,该图像显示在下一个活动中,称为Crop.class
这是我的logcat
12-12 13:26:36.340: E/AndroIDRuntime(23575): FATAL EXCEPTION: main 12-12 13:26:36.340: E/AndroIDRuntime(23575): Process: com.example.cobaandroID,PID: 23575 12-12 13:26:36.340: E/AndroIDRuntime(23575): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,request=1,result=-1,data=Intent { act=inline-data (has extras) }} to activity {com.example.cobaandroID/com.example.cobaandroID.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.net.Uri.toString()' on a null object reference 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread.deliverResults(ActivityThread.java:3368) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread.handleSendResult(ActivityThread.java:3411) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread.access00(ActivityThread.java:138) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.os.Handler.dispatchMessage(Handler.java:102) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.os.Looper.loop(Looper.java:136) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread.main(ActivityThread.java:5050) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at java.lang.reflect.Method.invoke(Native Method) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-12 13:26:36.340: E/AndroIDRuntime(23575): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.net.Uri.toString()' on a null object reference 12-12 13:26:36.340: E/AndroIDRuntime(23575): at com.example.cobaandroID.MainActivity.onActivityResult(MainActivity.java:226) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.Activity.dispatchActivityResult(Activity.java:5433) 12-12 13:26:36.340: E/AndroIDRuntime(23575): at androID.app.ActivityThread.deliverResults(ActivityThread.java:3364) 12-12 13:26:36.340: E/AndroIDRuntime(23575): ... 9 more
我有一个问题要打开/使用在大多数AndroID设备上工作的相机,这个项目的主要目标在很大程度上取决于使用相机.请给我你的帮助,谢谢..
解决方法 尝试下面的代码,Intent i = new Intent(androID.provIDer.MediaStore.ACTION_IMAGE_CAPTURE); file file=getoutputMediafile(1); picUri = Uri.fromfile(file); // create i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file startActivityForResult(i,CAPTURE_IMAGE);
其中getoutputMediafile(int)将是,
/** Create a file for saving an image */private file getoutputMediafile(int type){ file mediaStorageDir = new file(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES),"MyApplication"); /**Create the storage directory if it does not exist*/ if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ return null; } } /**Create a media file name*/ String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); file mediafile; if (type == 1){ mediafile = new file(mediaStorageDir.getPath() + file.separator + "img_"+ timeStamp + ".png"); } else { return null; } return mediafile;}
最后,
@OverrIDepublic voID onActivityResult(int requestCode,Intent data) { if (resultCode == Activity.RESulT_OK) { Intent i; switch (requestCode) { case CAPTURE_IMAGE: //THIS IS YOUR Uri Uri uri=picUri; break; } } }
干杯….:)
总结以上是内存溢出为你收集整理的使用Android相机拍摄图像后的空指针全部内容,希望文章能够帮你解决使用Android相机拍摄图像后的空指针所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)