【发布时间】: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