java - onActivityResult byte[] data 不管图片多大都返回null

标签 java android null arrays

我正在尝试使用 startActivityonActivityResult 在 Activity 之间传递一个字节数组。返回 onActivityResult 时字节数组为 null,我不知道为什么。无论字节数组有多大,都会发生这种情况,所以我认为这与大小无关。此外,我在另一个区域成功地使用 Intent 传递了一个大小大致相同的字节数组。代码:

在帖子 Activity 中:

public void callCropperIntent() {
         /* call the cropper to crop a photo from the gallery */
        intent = new Intent(getApplicationContext(), Cropper.class);
        startActivityForResult(intent, CROP_GALLERY_PICTURE);
    }

在裁剪 Activity 中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    File croppedImageFile = new File(getFilesDir(), "test.jpg");
    try {
        if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) {

            /** When the user is done picking a picture, we'll start the CropImage Activity,
             *  setting the output image file and size to 640 X 640 pixels square.
             */

            Uri croppedImage = Uri.fromFile(croppedImageFile);
            CropImageIntentBuilder cropImage = new CropImageIntentBuilder(640, 640, croppedImage);
            cropImage.setSourceImage(data.getData());
            cropImage.setOutputQuality(100);
            startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
        }
        else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) {
            /* when we are done cropping, send it back to PostActivity */
            //imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath()));
            Bitmap bmp = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

            //this set bitmap works so I know the bitmap is valid
            //test
            //imageView.setImageBitmap(bmp);

            byte[] imageData = stream.toByteArray();
            //bmp.recycle();

            Intent intent = getIntent();
            intent.putExtra("image", imageData);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

返回帖子 Activity :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    byte[] croppedData;

    /* check the request code */
    if (requestCode == CROP_GALLERY_PICTURE) {
        /* ensure the request was successful */
        if (resultCode == RESULT_OK) {
            /* The user picked and cropped a photo */

            /* retrieve photo from cropping activity */
            Intent intent = getIntent();
            croppedData = data.getByteArrayExtra("image");

            /* bytes ready to be sent to pg. 2 of posting process */
            callPostActivityPg2Intent(croppedData);
        }
    }
}

最佳答案

我认为在 cropper 类中,在 finish() 之前,您应该创建一个 Intent 类的新实例,而不是通过调用 getintent() 来获取实例。

例如:Intent intent = new Intent(); intent.putExtra()....

关于java - onActivityResult byte[] data 不管图片多大都返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27710770/

相关文章:

android - 如何在 Android 的 ListView 中打印一个值?

android - 短信检索器 API 无法获取以\u200b\u200b 开头的短信信息

php - Laravel 从表单输入中将 "null"插入数据库。

java - 获取完整的联系人电话号码和国家/地区代码,即使未输入国家/地区代码

java - 在三元中使用无界泛化静态函数时如何避免未经检查的强制转换?

android - 我可以使用 setClipBounds 裁剪带有圆圈的 View 吗?

c++ - 堆叠推和弹出功能不起作用

php - 如何将实际的 NULL 值插入到可为空的列中?

java - java 中的 perl CBC DES 等效项

java - 如何正确关闭java程序【初级】