我正在实现一个彩信应用程序,我正在使用设备相机。在拍摄照片后,我在图像显示屏幕中按保存后出现以下异常。
06-06 17:27:13.640: E/AndroidRuntime(21896): FATAL EXCEPTION: main
06-06 17:27:13.640: E/AndroidRuntime(21896): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=500, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.TakePic}: java.lang.NullPointerException
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.os.Looper.loop(Looper.java:130)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-06 17:27:13.640: E/AndroidRuntime(21896): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:27:13.640: E/AndroidRuntime(21896): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 17:27:13.640: E/AndroidRuntime(21896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-06 17:27:13.640: E/AndroidRuntime(21896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-06 17:27:13.640: E/AndroidRuntime(21896): at dalvik.system.NativeStart.main(Native Method)
06-06 17:27:13.640: E/AndroidRuntime(21896): Caused by: java.lang.NullPointerException
06-06 17:27:13.640: E/AndroidRuntime(21896): at com.example.nirbhaya.TakePic.onActivityResult(TakePic.java:73)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-06 17:27:13.640: E/AndroidRuntime(21896): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-06 17:27:13.640: E/AndroidRuntime(21896): ... 11 more
和代码::
package com.example.nirbhaya;
public class TakePic extends Activity {
private static final int CAMERA_REQUEST = 500;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takepic);
this.imageView = (ImageView) findViewById(R.id.imgLoad);
Button bt = (Button) findViewById(R.id.btCam);
Button picok = (Button) findViewById(R.id.bPicOk);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
int count = 0;
// here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
count++;
String file = dir+count+".jpg";
String photo = file;
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
请帮我解决。谢谢