MediaStore.ACTION_IMAGE_CAPTURE给出NullPointerException

时间:2013-06-06 12:03:55

标签: android android-camera mms

我正在为我使用设备相机实现一个MMS应用程序。拍下照片后,按下图像显示屏后保存后出现波纹异常。

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

和code ::

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);
    }
}
}
请帮我解决。 感谢

2 个答案:

答案 0 :(得分:0)

你错了,因为你做错了。

如果您使用相机意图传递额外参数MediaStore.EXTRA_OUTPUT,则相机活动会将捕获的图像写入该路径,并且不会在onActivityResult方法中返回位图。

如果您要检查通过的路径,那么您将知道实际上相机已在该路径中写入捕获的文件。

答案 1 :(得分:0)

我遇到了同样的问题但使用以下代码修复了

 Write folllowing code before `onCreate()`

 File output;

onClickListner

中写下以下代码
 Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
 output = new File(dir, "CameraContentDemo.jpeg");
 i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
 startActivityForResult(i, 1);

内部onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
           imgUpload.setImageURI(Uri.fromFile(output));
         }
}

参考链接:

Answer given by CommonsWare

Sample project provided by CommonsWare