Android用相机拍照并保存到sqlite会导致java.lang.IllegalStateException:无法压缩回收的位图

时间:2018-03-21 05:32:03

标签: android sqlite bitmap camera

Android应用尝试使用内容解析器在使用相机拍照后将图像保存到sqlite数据库。

然而,无论我做什么,我仍然会得到“无法压缩再循环位图”的变体

我查看了stackoverflow上的很多帖子,但没有一个解决我的问题。我似乎无法找到我的位图被回收的代码导致错误:

Caused by: java.lang.IllegalStateException: Can't compress a recycled bitmap
    at android.graphics.Bitmap.checkRecycled(Bitmap.java:394)
    at android.graphics.Bitmap.compress(Bitmap.java:1077)
    at com.wernerraubenheimer.ujcp.fragments.UserInputFragment.getBytes(UserInputFragment.java:269)
    at com.wernerraubenheimer.ujcp.fragments.UserInputFragment.saveImage(UserInputFragment.java:248)
    at com.wernerraubenheimer.ujcp.fragments.UserInputFragment.onActivityResult(UserInputFragment.java:241)

我的代码:

private void takePhotoFromCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == getActivity().RESULT_CANCELED) {
        return;
    }
    if (requestCode == GALLERY) {
        if (data != null) {
            Uri contentURI = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentURI);
                saveImage(bitmap);
                CircleTransform circleTransform = new CircleTransform();
                avatarView.setImageBitmap(circleTransform.transform(bitmap));

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "Failed!", Toast.LENGTH_LONG).show();
            }
        }
    } else if (requestCode == CAMERA) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        CircleTransform circleTransform = new CircleTransform();
        avatarView.setImageBitmap(circleTransform.transform(thumbnail));
        saveImage(thumbnail);
    }
}

public void saveImage(Bitmap myBitmap) {

    ContentValues userValues = new ContentValues();
    userValues.put(ProgrammeCourseContract.LearnersEntry.COLUMN_AVATAR, getBytes(myBitmap));

    if(!HAS_AVATAR) {
        userValues.put(ProgrammeCourseContract.LearnersEntry._ID, 1); // Only one user for now, future may include more
        getActivity().getContentResolver().insert(
                ProgrammeCourseContract.LearnersEntry.CONTENT_URI,
                userValues);

    } else { //there is already an avatar image, save the new one
        getActivity().getContentResolver().update(
                ProgrammeCourseContract.LearnersEntry.CONTENT_URI,
                userValues,
                ProgrammeCourseContract.LearnersEntry._ID + " = ?",
                new String[]{"1"});
    }
    onAvatarUpdateListener.onAvatarUpdate();
}

// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}

// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

1 个答案:

答案 0 :(得分:0)

根据Mike M,将saveImage(缩略图)移至CircleTransform之前:

else if (requestCode == CAMERA) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            saveImage(thumbnail);
            CircleTransform circleTransform = new CircleTransform();
            avatarView.setImageBitmap(circleTransform.transform(thumbnail));
        }