Android Can';在解析时保存回收的位图时,不要压缩它

Android Can';在解析时保存回收的位图时,不要压缩它,android,bitmap,parse-platform,Android,Bitmap,Parse Platform,我正在开发一个应用程序,我想从智能手机的图库中拍摄一张照片,然后裁剪它,接下来我想把它保存在Parse.com上 但我在尝试在解析时保存图片时遇到了一个问题,出现了一个错误“无法压缩回收的位图” 以下是我从gallery中检索图像并将其上载到ImageView的代码: Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.blink); profil.startAnimation(hypersp

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

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

以下是我从gallery中检索图像并将其上载到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);
        }
    }
}   
然后我试图保存位图文件“Photo”,但我不能这样做

下面是将照片位图保存到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();        
        }
    }
});

bitmap=photo
看起来像是问题所在,这就是它无法压缩的原因。您确定照片是有效的对象吗?您可以使用BitmapFactory()方法获取图像引用,甚至可以直接通过路径。