有效加载大型Bitmap以从库中选择动态图像?

时间:2015-05-18 10:32:42

标签: android bitmap

我想在Imageview中加载大型Bitmap图像。我遵循Loading Large Bitmaps Efficiently的代码。它显示单个imageView.But我想从图库中选择动态ImageView的图像。我试着获取id选择的图像和设置decodeSampledBitmapFromResouce.It在ImageView中设置空。请帮我解决问题。任何帮助我非常感谢。请在下面看我的代码。

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);
}

1 个答案:

答案 0 :(得分: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;
}