当前位置:编程学习 > wap >>

旋转图片报错:java.lang.OutOfMemoryError


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 左右。
请问高手怎么解决啊?还有没有其他旋转图片的方法? matrix java 网络 --------------------编程问答-------------------- 图片太大, 就对其先进行按比例压缩, 压到图片的1/2或者1/4, 再旋转 --------------------编程问答--------------------
引用 1 楼 intuition444 的回复:
图片太大, 就对其先进行按比例压缩, 压到图片的1/2或者1/4, 再旋转


压缩就模糊了,不想用压缩。 --------------------编程问答-------------------- 我这么跟你说吧, 你在android上用"图库"看到的任何一张图片都不是原图,都是缩略图。 --------------------编程问答--------------------
引用 3 楼 intuition444 的回复:
我这么跟你说吧, 你在android上用"图库"看到的任何一张图片都不是原图,都是缩略图。

"图库"读取本地图片可以这样设置:
opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
Bitmap bitmap = BitmapFactory.decodeFile(filePath,opts);

但旋转图片Bitmap.createBitmap()没有 opts.inSampleSize 这个设置。
--------------------编程问答-------------------- scal之后文件的size就变小了, 可是你现在的问题变了?
scal-rotate->decode 很清晰阿, 你之前怎么做还怎么做呗。 --------------------编程问答-------------------- 可以试试用jni进行旋转返回结果显示 --------------------编程问答-------------------- 很纠结。。。。 --------------------编程问答-------------------- 内存问题一直纠结啊 --------------------编程问答-------------------- 不一定非得选择bitmap ,你可以选择view试试  例如使用旋转动画 --------------------编程问答-------------------- 如果你不想增加分辨率 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;
}





  --------------------编程问答-------------------- 把 Bitmap.createBitmap(tmpBitmap,0,0, x, y,matrix, true); 
替换成 Bitmap.createBitmap(tmpBitmap,0,0, y, x,matrix, true); 
当把照片旋转90度,宽和高就相同。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,