こんにちは私はSDカード上の画像のパスのセットを持っています、すべての画像は1024*768ピクセルのように大きいです。
これらすべての画像を、比較的縮小された画像とともにグリッドビューで表示する必要があります。最初にグリッドビューを表示してから、生成された縮小画像を読み込む必要があります。どうすればこれを達成できますか。
今ここにあります:
すべての画像パスを取得し、
File imgFile = new File(pathToImageOnSD);
if (imgFile.exists()) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
item.bitmap = myBitmap;
imageview.setImageBitmap(myBitmap);
}
画像パスごとにこれを行っていますが、メモリエラーで終了しました。URLから画像をロードするライブラリがありますが、SDカードにローカル画像をロードするための同様のライブラリが必要です。
編集:
public Bitmap loadBitmapFromPath(File f) {
// decodes image and scales it to reduce memory consumption
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
if (scale >= 2) {
scale /= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
上記の方法を使用してビットマップを縮小しましたが、完全に機能しました。しかし、私の問題は、グリッドをスクロールするとビューが再描画され、フローがスムーズにならないことです。これらのビットマップをキャッシュする方法と、ビューが完全に読み込まれるのを待つ必要がないように遅延ロードする方法です。