android的Bitmap子映像

时间:2015-06-07 22:19:21

标签: android listview bitmap subsampling

在SD卡的列表视图中显示图像时出现内存不足错误

我知道它是因为我需要对位图进行二次采样,因为它们是大型的8mp图像,如果我将它们缩小到600 x 450则会完全加载

我使用以下

正常加载它们
  Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + text7[i]);
  item_details.setImage(bmp);

正如我所说,如果我使用全尺寸图像,这会导致应用程序崩溃

我有以下两种方法,它们对位图进行了子样本

  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
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;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;

}

问题是我不知道如何调用它以便显示

这是我填写listview的方法

private ArrayList<ListItemDetails> GetSearchResults() {
    // TODO Auto-generated method stub
    ArrayList<ListItemDetails> results = new ArrayList<ListItemDetails>();
    ImageView imageview = (ImageView) findViewById(R.id.imageView1);
    DBAdapter db = new DBAdapter(this);
    db.open();
    Cursor c = db.getAsset3();
    String[] text1 = new String[c.getCount()];
    String[] text2 = new String[c.getCount()];
    String[] text3 = new String[c.getCount()];
    String[] text4 = new String[c.getCount()];
    String[] text5 = new String[c.getCount()];
    String[] text6 = new String[c.getCount()];
    String[] text7 = new String[c.getCount()];
    for(int i = 0; c.moveToNext(); ++i)
    {
        text1[i] = c.getString(0);
        text2[i] = c.getString(1);
        text3[i] = c.getString(2);
        text4[i] = c.getString(3);
        text5[i] = c.getString(4);
        text6[i] = c.getString(5);
        text7[i] = c.getString(6);
       }
     c.close();

    for(int i=0;i<text1.length;i++)
    {
        item_details= new ListItemDetails();
        item_details.setSR1("School: " + text1[i]);
        item_details.setSR2("Building: " + text2[i]);
        item_details.setSR3("Floor: " + text3[i]);
        item_details.setSR4("Room: " + text4[i]);
        item_details.setSR5("Drawing Ref: " + text5[i]);
        item_details.setSR6("Fault: " + text6[i]);
        item_details.setSR7("Picture Filename: " + text7[i]);
        String picFormat="Harris%d.jpg";
        String pic = String.format(picFormat, i+1);
 /////////////////////////////////////////////////////////////////           
 /// Load bitmap     

imageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), 
R.id.imageView1, 100, 100));

        results.add(item_details);
    }

    return results;
    }

它似乎工作,因为列表视图加载,每个项目上的所有文本都在那里,但图像是空白

任何我出错的想法?​​

任何帮助表示赞赏

标记

2 个答案:

答案 0 :(得分:1)

您没有将图像文件名传递给decodeSampledBitmapFromResource,那么如何知道要加载哪个文件?您传入的资源ID是ImageView的资源ID,而不是图像资源的资源ID。

如果要从给定文件名而非资源的文件加载图像,则需要使用BitmapFactory.decodeFile()而不是BitmapFactory.decodeResource更改decodeSampledBitmapFromResource从文件加载,例如这样:

public static Bitmap decodeSampledBitmapFromFile(String filename,
    int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}

然后更改您的ArrayList代码以将文件名传递给它:

item_details.setImageBitmap(decodeSampledBitmapFromFile("/storage/emulated/0/Pictures/" + text7[i], 100, 100));

答案 1 :(得分:-1)

你可以使用Picasso http://square.github.io/picasso/,这很简单,你可以避免很多麻烦。