0

在片段中,我需要使用相机来捕获图像并在图像视图中预览。但它因以下失败交付结果 ResultInfo错误而崩溃。

public class Add_SPOT2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = inflater.inflate(R.layout.add_spot_2, container, false);

        upload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                String path = Environment.getExternalStorageDirectory()
                        + "/spot";
                File photopath = new File(path);
                if (!photopath.exists()) {
                    photopath.mkdir();
                }

                File imagePath = new File(photopath, System.currentTimeMillis()+ ".png");

                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(imagePath));
                imageUri = Uri.fromFile(imagePath);

                startActivityForResult(intent, 100);

            }
        });

        return v;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if( requestCode == 100 ) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");  // Error occured.
            uploadImage.setImageBitmap(photo);oto));
        }

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onLowMemory() {
        // TODO Auto-generated method stub
        super.onLowMemory();
    }
}

这是异常日志

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131172, result=-1, data=null} to activity {com.fssd.spot/com.fssd.spot.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3410)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3453)
    at android.app.ActivityThread.access$1200(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5283)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.fssd.spot.setting.Add_SPOT2.onActivityResult(Add_SPOT2.java:92)
    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
    at android.app.Activity.dispatchActivityResult(Activity.java:5472)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
    ... 11 more

请帮我解决问题。

4

1 回答 1

0

由于您已经向uri相机应用程序提供了,我猜相机应用程序不会用位图填充数据。因此你得到NullPointerException. 所以改变你的代码如下

 if( requestCode == 100 && resultCode == Activity.RESULT_OK) {
           uploadImage.setImageURI(imageUri));
  }
于 2013-09-17T19:01:44.127 回答