[英]How to use bitmap factory to compress image without reduce the quality taken from camera?
I have 3 images taken from camera and I want to set to 3 ImageView. 我有3张从相机拍摄的图像,我想设置为3张ImageView。 But it throws OutOfMemory because the image has large size. 但是它会抛出OutOfMemory,因为图像的大小很大。 I've read that bitmap factory can compress the size without reducing the quality. 我读过,位图工厂可以压缩大小而不会降低质量。 The problem is I don't know how to do that. 问题是我不知道该怎么做。
This is my onClick Button: 这是我的onClick按钮:
openCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"imagename.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent,0);
dialog.cancel();
}
});
And this is my onActivityResult: 这是我的onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//result from camera
try{
if (requestCode==0 && resultCode==RESULT_OK){
//bitmap = (Bitmap)data.getExtras().get("data");
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outPutfileUri);
drawable = new BitmapDrawable(getResources(), bitmap);
imageSelfie.setImageDrawable(drawable);
}
} catch (Exception ignored){}}
Anyone can help me? 有人可以帮助我吗?
Use like this : 像这样使用:
File file = new File(Environment.getExternalStorageDirectory(),"imagename.jpg");
fileDeleted = !file.exists() || file.delete();
if (fileDeleted) {
FileOutputStream out = new FileOutputStream(file);
if (imageToSave != null) {
imageToSave.compress(Bitmap.CompressFormat.JPEG, 65, out);
}}
out.flush();
out.close();
Instead of using the media store which tries to give you a full bitmap you better open an inputstream and load a resized bitmap from it. 与其使用试图为您提供完整位图的媒体存储,不如您最好打开输入流并从中加载调整大小的位图。
InputStream is = getContentResolver().openInputStream(outPutfileUri);
Then use BitmapFactory.decodeStream() with an options parameter where you scale down the image several times. 然后,将BitmapFactory.decodeStream()与options参数一起使用,在其中可以将图像缩小几次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.