我正在开发一个应用程序,我想从智能手机的图库中拍照,然后裁剪它,接下来我想将它保存在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);
}
}
}
然后我试图保存位图文件“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();
答案 0 :(得分:0)
我不确定这是否能解决您的问题,但是要成功保存解析文件,您需要等待文件保存,然后再尝试将文件存储在解析对象中,即
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();
}
}
});