压缩和解压缩位图-'失败的绑定交易'

时间:2018-09-05 09:01:13

标签: java android bitmap

我想在拍照后或从图库中获取图像数据后再传递给另一个活动。但是我遇到了“失败的绑定交易”错误,所以我做了一些研究,我认为这是因为文件太大了,我需要压缩它并在另一个活动中解压缩。但是解压缩代码似乎不起作用,图像被像素化并模糊。

Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

为什么?如何解决?

第一个活动

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    inputStreamImg = null;

    if (resultCode == getActivity().RESULT_OK && data != null) {
        if (requestCode == PICK_IMAGE_CAMERA) {
            try {
                Uri selectedImage = data.getData();
                bitmap = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bytes);
                byte[] byteArray = bytes.toByteArray();
                goProgressDetailPage(byteArray);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == PICK_IMAGE_GALLERY) {
            Uri selectedImage = data.getData();

            if (selectedImage==null){
                return;
            }

            try {
                bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), selectedImage);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bytes);
                byte[] byteArray = bytes.toByteArray();
                goProgressDetailPage(byteArray);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public void goProgressDetailPage(byte[] byteArray){
    Intent i = new Intent(getContext(), EditProgressActivity.class);
    i.putExtra("viewString",byteArray);
    getContext().startActivity(i);
}

第二活动

byte[] bytes = intent.getByteArrayExtra("viewString");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
//decompress fail, the image is blur
base64img = Base64.encodeToString(bytes, Base64.DEFAULT);
Log.d("image uploaded", ""+base64img);
//upload base64 to server...

相关问题:

https://stackoverflow.com/questions/3528735/failed-binder-transaction-when-putting-an-bitmap-dynamically-in-a-widget#=

Still get error FAILED BINDER TRANSACTION although have compressed it

1 个答案:

答案 0 :(得分:0)

如你所说,

您需要在捕获图像后传递图像。因此,概念是您只需拍摄照片并将其保存到设备中并获取图像位置的路径。现在只需在整个活动中通过路径即可。

根据您的需要,您不必压缩或解压缩图像。