【问题标题】:Load large Bitmap efficiently for dynamic image pick from gallery?有效地加载大型位图以从图库中选择动态图像?
【发布时间】:2015-05-19 22:52:21
【问题描述】:

我想在 Imageview 中加载大的位图图像。我遵循来自 Loading Large Bitmaps Efficiently 的代码。它显示为单个 imageView。但我想从图库中选择动态 ImageView 的图像。我尝试获取选择 Image 并设置 decodeSampledBitmapFromResouce。它在 ImageView 中设置了 Empty。请帮我解决问题。非常感谢任何帮助。请在下面查看我的代码。

Button loadImg;
ImageView myImageView;
InputStream imageStream;
Bitmap productIndex = null;

private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadImg = (Button)findViewById(R.id.btnPickImage);
    myImageView = (ImageView) findViewById(R.id.myImgView);

    loadImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,RESULT_LOAD_IMAGE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        myImageView = (ImageView) findViewById(R.id.myImgView);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(picturePath, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        String imageType = options.outMimeType;

// here I set the bitmap to ImageView but the Image is not shown 
        int picId = getResources().getIdentifier(picturePath, "drawable", getApplicationContext().getPackageName());
        myImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),picId,100,100));

    }
}

public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;

    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        while ((halfWidth / inSampleSize) > reqWidth && (halfHeight / inSampleSize) > reqHeight) {
            inSampleSize *= 2;
        }

    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res,resId,options);

    /* Calculate inSampleSize */
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res,resId,options);
}

【问题讨论】:

  • 你的图片来自文件而不是资源,对吧?
  • @vincentzhou 是的,myImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),picId,100,100)); 在这里 picId 我尝试获取所选择图像的 id。我认为这不正确..

标签: android bitmap


【解决方案1】:

试试这个。

public static Bitmap decodeSampledBitmapFromFile(File f, int reqWidth, int reqHeight) { // BEST QUALITY MATCH
    String path = f.getAbsolutePath();

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    // Raw height and width of background
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }

    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    // Correct rotation
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(f.getPath());
    } catch (IOException e) {
        e.printStackTrace();
    }
    int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    int rotationInDegrees = exifToDegrees(rotation);
    Matrix matrix = new Matrix();
    if (rotation != 0f) {
        matrix.preRotate(rotationInDegrees);
    }

    Bitmap output = BitmapFactory.decodeFile(path, options);
    if (output != null) {
        return Bitmap.createBitmap(output, 0, 0, output.getWidth(), output.getHeight(), matrix, false);
    } else {
        return null;
    }
}

public static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}

【讨论】:

  • 兄弟exifToDegrees(rotation) 出错。 exifToDegrees无法解决方法错误..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多