【问题标题】:Can't compress a recycled bitmap when saving it on Parse在 Parse 上保存时无法压缩回收的位图
【发布时间】:2015-02-04 23:14:44
【问题描述】:

我正在开发一个应用程序,我想从智能手机的图库中拍照然后裁剪它,然后我想将它保存在 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();

【问题讨论】:

  • bitmap = photo 看起来像是无法压缩的问题。你确定照片是有效的对象吗?您可以使用 BitmapFactory() 方法获取图像参考,甚至可以直接通过路径。

标签: android bitmap parse-platform


【解决方案1】:

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

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();        
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多