如何使用大位图.旋转并插入画廊

Arc*_*tos 5 memory camera android image

我需要用相机拍照,如果取决于图片大小,请先将其旋转,然后再将其保存到图库中.

我正在使用

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri); startActivityForResult(imageCaptureIntent,IMAGE_CAPTURE);

拍摄照片并将其保存到临时文件中.

然后

位图bmp = BitmapFactory.decodeFile(imagePath);
String str = android.provider.MediaStore.Images.Media.insertImage(cr,bmp,name,description);

保存它.

这是我试图用来旋转位图的代码

Matrix matrix = new Matrix();
matrix.postRotate(180);
Bitmap x = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
android.provider.MediaStore.Images.Media.insertImage(cr,x,name,description);

问题是我得到一个OutOfMemoryException.

是否有更好的方法来处理位图以避免破坏内存?

〜先谢谢,问候

Gal*_*lal 2

我认为没有更好的方法来处理位图。您可以尝试直接从文件中将数据解析为 Byte[] 的一部分,然后分段操作;这很困难,你可能会得到非常难看的代码。

我还提出以下建议:

  • 使用这种方式android.provider.MediaStore.Images.Media.insertImage(cr, imagePath, name, description)代替android.provider.MediaStore.Images.Media.insertImage(cr, bmp, name, description)不需要调用Bitmap bmp = BitmapFactory.decodeFile(imagePath),并且此时不会将位图加载到内存中。

  • 在整个代码中,确保除非需要,否则不会加载位图。设置不再需要的位图null并调用垃圾收集器,或使用bmp.recycle().