我需要用相机拍照,如果取决于图片大小,请先将其旋转,然后再将其保存到图库中 .
我正在使用
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 .
是否有更好的方法来处理位图以避免破坏内存?
〜先谢谢,问候
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()
.我在旋转位图时遇到了同样的问题 . 问题在这里:
是它创建2位图所以他们想要x2更多的RAM .
检查my question and solution here . 我打赌ImageMagick lib .