onActivityResult返回data = null

时间:2012-02-10 17:14:28

标签: java android android-intent

好的,这就是我发送的意图

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE);

然后在onActivityResult中我这样做:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("Intent name:",data.toString());
        if (requestCode == REQUEST_CODE){
            if (resultCode == Activity.RESULT_OK){
                Toast.makeText(this, "Image saved to \n" + fileUri.toString() , Toast.LENGTH_LONG).show();
                Toast.makeText(this, "Result Code: " + resultCode , Toast.LENGTH_LONG).show();
                //Bitmap mBitMap = BitmapFactory.decodeFile(data.getData().toString());
                //imageView.setImageBitmap(mBitMap);
            }
            else if (resultCode == RESULT_CANCELED){
                Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG).show();
            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }  

LogCat在显示Image Saved ....的行显示NullPointerException 还有这个:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

无论我是尝试使用data对象还是我班级的fileUri字段,都会发生这种情况。
为什么要返回数据null
为什么即使我使用该类的字段我仍然会得到相同的错误?

2 个答案:

答案 0 :(得分:8)

每当您通过传递带有相机意图的 EXTRAOUTPUT 来保存图像时,

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

在文件中,onActivityResult内的数据参数始终返回null。因此,不使用数据来检索图像,而是使用文件路径来检索Bitmap

所以onActivityResult会是这样的:

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

    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            String[] fileColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(imageUri,
                fileColumn, null, null, null);
            String contentPath = null;
            if (cursor.moveToFirst()) {
                contentPath = cursor.getString(cursor
                    .getColumnIndex(fileColumn[0]));

                Bitmap bmp = BitmapFactory.decodeFile(contentPath);
                ImageView img = (ImageView) findViewById(R.id.imageView1);
                img.setImageBitmap(bmp);


            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG)
                .show();
            } else {
                Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG)
                .show();
            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

确保您已将imageUri或fileUri作为全局变量,以便您也可以在onActivityResult内访问它。祝你好运

答案 1 :(得分:0)

在这些情况下处理数据的正确/首选方式如下:

在调用Activity中将数据设置为Intent,然后将setResult代码设置为RESULT_OK,然后完成该活动。

在此接收活动中,检查结果代码..并从Intent变量中检索数据:intent.getExtra("... "); //您已在子活动中设置的变量现已关闭..