android - 旋转图片错误: java. lang.OutOfMemoryError

标签 android rotation

public Bitmap RotationPic(Bitmap tmpBitmap){
        int x = tmpBitmap.getWidth();
        int y = tmpBitmap.getHeight();
        if(x > y){

            Matrix matrix = new Matrix();
            matrix.reset();
            matrix.setRotate(90);
            return Bitmap.createBitmap(tmpBitmap,0,0, x, y,matrix, true);
        }
        return tmpBitmap;
    }

这句话 Bitmap.createBitmap(tmpBitmap,0,0, x, y,matrix, true);有错误。

我看了网络图片,图片尺寸很大,大约100k~300k。

如何解决这个问题?还有其他方法可以旋转图片吗?

最佳答案

试试这个 但你需要 pic 的路径才能使用它。 如果您不喜欢分辨率,请增加 REQUIRED_SIZE 值

public static Bitmap DecodeFileToBitmap (String path)
{
    try 
    {       
            File f = new File(path);

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            final int REQUIRED_SIZE=500;

            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                scale*=2;

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            Bitmap bitmap = bm;

            ExifInterface exif = null;
            try 
            {
                exif = new ExifInterface(path);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            Matrix m = new Matrix();

            if ((orientation == 3)) 
            {
                m.postRotate(180);
                m.postScale((float) bm.getWidth(), (float) bm.getHeight());
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
                return bitmap;
            } 
            else if (orientation == 6) 
            {
                m.postRotate(90);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
                return bitmap;
            }
            else if (orientation == 8) 
            {
                m.postRotate(270);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), m, true);
                return bitmap;
            }   

            return bitmap;
            }   

            catch(Exception ex) {}
    } 

    catch (FileNotFoundException e) {}
    return null;
}

关于android - 旋转图片错误: java. lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15020890/

相关文章:

android - Visualizer onFftDataCapture - 如何解释?

android - 如何解决eclipse ide中ADT更新的依赖问题?

c++ - 在 Irrlicht 中移动和旋转 SceneNode(Sphere)

javascript - 使用 new Image() 在 JavaScript 中旋转图像

c++ - 围绕一个点(行星)向不同方向移动

iOS 7 在标签栏中旋转 View 时, View 右侧不可点击

java - 如何在屏幕底部显示对话框

android - 错误 : Unable to find file with path: prelude_dev. js

java - GC_EXTERNAL_ALLOC 和 GC_FOR_MALLOC 日志消息每秒生成一次

android - RelativeLayout 的旋转如何在 android 中工作?