onActivityResult数据返回

onActivityResult数据返回

本文介绍了onActivityResult数据返回= NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以这这里是我送的意图

Ok so this here is the intent I am sending

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我这样做:

And then in the onActivityResult I am doing this:

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中呈现出NullPointerException异常的,说图片保存行....
而也这一点:
java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 100,结果= -1,数据= NULL}

The LogCat is showing a NullPointerException at the line that says Image Saved....
And also this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

这会发生,我是否尝试使用数据对象或我的类的了fileURI 字段。
为什么要返回的数据
为什么即使我使用的是类的领域我仍然得到同样的错误?

This happens whether i try to use the data object or the fileUri field of my class.
Why is data being returned null?
Why is it that even though I am using a field of the class i still get the same error?

推荐答案

当你通过传递保存图像的 EXTRAOUTPUT 带摄像头的意图即

Whenever you save an image by passing EXTRAOUTPUT with camera intent ie

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

在一个文件,里面的数据参数 onActivityResult 总是返回null。而不是使用数据检索图像,这样,使用文件路径检索位图

in a file, the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the 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 访问它。祝您好运。

Make sure that you have taken imageUri or fileUri as a global variable so that you can access it inside onActivityResult as well. Best of luck

这篇关于onActivityResult数据返回= NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:30