小编nez*_*zbo的帖子

在没有OutOfMemoryError或缩减的情况下在Android中旋转图像

基本上我正在尝试在Android应用程序中旋转位图(来自图像).我想这样做的原因是,即使垂直拍摄,从相机拍摄的照片(通过意图)也会水平显示,并且方向在图像上保留为元数据.如果错了,请纠正我.然而问题是,如果在装有相当好的相机的手机上拍摄时,图像将占用大量内存,而且我没有找到旋转和保存它的方法而没有获得OutOfMemoryError的风险.下面的代码是我的地方:

  1. 加载图像
  2. 检查是否需要旋转
  3. 加载缩小版本以在ImageView中显示
  4. 如有必要,旋转小图像
  5. 在一个单独的线程; 加载,旋转和保存图像,以便将来不需要

对于应用程序而言,保持图像的分辨率非常重要,但欢迎任何带编码的技巧.我已经在互联网上搜索了几天,找不到比我已经实现的更多的内容.这里有另一个主题,但似乎没有任何解决方案.希望你能帮忙.

    public Bitmap getBitmap(final Context c) {
    if (bitmap != null)
        return bitmap;

    final int rotate = necessaryRotation(c, file);
    // if(rotate != 0) rotateImageFile(c, rotate);

    try {
        // Get scaled version
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, options);
        options.inSampleSize = calcInSampleSize(options, 1024, 1024);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, options);

        // rotate?
        bitmap = rotateImage(c,bitmap,rotate);

        System.out.println("Bitmap loaded from file: size="
                + bitmap.getWidth() + "," + bitmap.getHeight());

        System.gc();
    } catch (Exception …
Run Code Online (Sandbox Code Playgroud)

android bitmap rotation out-of-memory

6
推荐指数
1
解决办法
3348
查看次数

标签 统计

android ×1

bitmap ×1

out-of-memory ×1

rotation ×1