0

我正在实现一个彩信应用程序,我正在使用设备相机。在拍摄照片后,我在图像显示屏幕中按保存后出现以下异常。

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);
    }
}
}

请帮我解决。谢谢

4

2 回答 2

0

你错了,因为你做错了。

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

如果您检查您正在传递的路径,那么您将知道实际上相机已经在该路径中写入了捕获的文件。

于 2013-06-06T12:06:38.570 回答
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));
         }
}

参考链接:

CommonsWare 给出的答案

CommonsWare 提供的示例项目

于 2013-06-06T12:10:10.137 回答