在Android中仅加载部分位图文件

Sch*_*ger 16 graphics performance android

我想将一个位图图像的裁剪版本加载到Bitmap对象中,而不加载原始位图.

如果不编写自定义加载例程来处理原始数据,这是否可行?

谢谢,桑德尔

Ste*_*ley 13

这实际上非常简单.使用

Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)

更新:使用BitmapRegionDecoder

  • 谢谢史蒂夫,但这需要我首先加载sourceBitmap,我试图避免这样做,因为它太大而不适合内存,我想使用尚未下采样的位图. (4认同)

Ren*_*ith 10

试试这个

InputStream istream =   null;
try {
     istream = this.getContentResolver().openInputStream(yourBitmapUri);
} catch (FileNotFoundException e1) {
     e1.printStackTrace();
}

BitmapRegionDecoder decoder     =   null;
try {
    decoder = BitmapRegionDecoder.newInstance(istream, false);
} catch (IOException e) {
    e.printStackTrace();
}
Bitmap bMap = decoder.decodeRegion(new Rect(istream, x to start from, y to start from, x to end with, y to end with), null);    
imageView.setImageBitmap(bMap);
Run Code Online (Sandbox Code Playgroud)