在 Parse 上保存时无法压缩回收的位图

Can't compress a recycled bitmap when saving it on Parse

我正在开发一个应用程序,我想从智能手机的图库中拍照然后裁剪它,接下来我想将它保存在 Parse.com

但我在尝试保存图片时遇到问题,出现错误“无法压缩回收的位图”

这是我从图库中检索图像然后将其上传到 ImageView 的代码:

Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
profil.startAnimation(hyperspaceJumpAnimation);
Intent intent = new Intent();
 // call android default gallery
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 // ******** code for crop image
 intent.putExtra("crop", "true");
 intent.putExtra("aspectX", 200);
 intent.putExtra("aspectY", 200);
 intent.putExtra("outputX", 150);
 intent.putExtra("outputY", 150);
 intent.putExtra("scale", true);
 intent.putExtra("scaleUpIfNeeded", true);
 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

 try {
    intent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(intent,
    "Complete action using"), PICK_FROM_GALLERY);
 } catch (ActivityNotFoundException e) {
     // Do nothing for now
 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_FROM_CAMERA) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            photo = extras.getParcelable("data");
            TransitionDrawable td = new TransitionDrawable(new Drawable[]{
            new ColorDrawable(android.R.color.transparent),
            new BitmapDrawable(this.getResources(), getCircleBitmap(photo))
            });
            profil.setImageDrawable(td);
            td.startTransition(2000);
        }
    }
}   

然后我试图保存位图文件“照片”,但我无法做到这一点

这里是将图片位图保存到parse.com中的代码

final Bitmap bitmap = photo;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);  
byte[] image = stream.toByteArray();
file = new ParseFile("profil.png", image);
file.saveInBackground();

object.put("name", nom.getText().toString());                       
object.put("Profil",file);                     
object.saveInBackground();

我不确定这是否能解决您的问题,但是要在解析中成功保存文件,您需要等待文件保存,然后再尝试将文件存储在解析对象中,即

final Bitmap bitmap = photo;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);  
byte[] image = stream.toByteArray();
file = new ParseFile("profil.png", image);

file.saveInBackground(new SaveCallback() {  
    public void done(ParseException e) {   
        if(null == e){
            object.put("name", nom.getText().toString());                       
            object.put("Profil",file);                     
            object.saveInBackground();        
        }
    }
});