1

SDカードからリストビューで画像を表示するときにメモリ不足エラーの問題があります

ビットマップは大きな 8mp 画像であるためサブサンプリングする必要があり、それらを 600 x 450 に縮小すると完全にロードされるため、それはわかっています。

次を使用して通常どおりロードします

  Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + text7[i]);
  item_details.setImage(bmp);

私が言ったように、フルサイズの画像を使用するとアプリがクラッシュします

ビットマップをおそらくサブサンプリングする次の2つの方法があります

  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;

}

問題は、それを呼び出す方法がわからないため、表示されることです

これは、リストビューを埋めるための私の方法です

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

リストビューが読み込まれ、各アイテムのすべてのテキストがそこにあるが、画像が空白であるため、機能しているようです

私が間違っているアイデアはありますか?

どんな助けでも大歓迎

マーク

4

2 に答える 2

1

に画像ファイル名を渡していないdecodeSampledBitmapFromResourceので、ロードするファイルをどのように知るのでしょうか? 渡すリソース ID は、画像リソースのリソース ID ではなく、ImageView のリソース ID です。

リソースではなくファイル名を指定してファイルから画像をロードする場合は、次のように、 BitmapFactory.decodeResource の代わりに BitmapFactory.decodeFile() を使用してファイルからロードするように変更する必要あります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));
于 2015-06-08T02:07:09.423 に答える
-2

Picasso http://square.github.io/picasso/を使用できます。これは非常にシンプルで、多くの頭痛を回避できます。

于 2015-06-08T02:15:18.653 に答える