如何使用位图工厂压缩图像而不降低相机的质量? [英] How to use bitmap factory to compress image without reduce the quality taken from camera?
问题描述
我从相机拍摄了3张图片,我想设置为3张ImageView。但它会抛出OutOfMemory,因为图像尺寸很大。我已经读过位图工厂可以在不降低质量的情况下压缩尺寸。问题是我不知道该怎么做。
这是我的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();
}
});
这是我的onActivityResult:
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
//来自相机的结果
尝试{
if(requestCode == 0&& resultCode == RESULT_OK){
// bitmap =(Bitmap)data.getExtras( )获得( 数据);
bitmap = MediaStore.Images.Media.getBitmap(getActivity()。getContentResolver(),outPutfileUri);
drawable = new BitmapDrawable(getResources(),bitmap);
imageSelfie.setImageDrawable(drawable);
}
} catch(忽略异常){}}
任何人都可以提供帮助我?
而不是使用试图为您提供完整位图的媒体商店,您最好打开输入流并加载调整大小来自它的位图。
InputStream is = getContentResolver()。openInputStream(outPutfileUri);
然后使用带有选项参数的BitmapFactory.decodeStream(),您可以多次缩小图像。 / p>
I have 3 images taken from camera and I want to set to 3 ImageView. But it throws OutOfMemory because the image has large size. 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:
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:
@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?
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.
这篇关于如何使用位图工厂压缩图像而不降低相机的质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!