內存不足我想在安卓 對於此實現這一點,我必須從資源文件夾載入圖像,我在XML文件中做兩個HorizontalScrollView
和它動態地加載ImageView
。對於裝載的ImageView我使用此代碼位圖的Android
LinearLayout myGallery = (LinearLayout) findViewById(R.id.mygallery);
try {
String galleryDirectoryName = "gallery";
String[] listImages = getAssets().list(galleryDirectoryName);
for (String imageName : listImages) {
InputStream is = getAssets().open(galleryDirectoryName + "/" + imageName);
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bitmap);
LinearLayout.LayoutParams myGallery1= new LinearLayout.LayoutParams(100, 100);
myGallery1.setMargins(20, 0, 10, 0);
//its is also working
// imageView.setLayoutParams(myGallery1);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// diplayImage.setImageBitmap(bitmap);
}
});
Log.e("GalleryWithHorizontalScrollView", e.getMessage(), e);
}
//重複上述代碼在第二horizontalScrollView加載的ImageView
LinearLayout myGallery2 = (LinearLayout) findViewById(R.id.mygallery2);
try {
String galleryDirectoryName1 = "gallery2";
String[] listImages2 = getAssets().list(galleryDirectoryName1);
for (String imageName : listImages2) {
InputStream is1 = getAssets().open(galleryDirectoryName1 + "/" + imageName);
Bitmap bitmap1 = BitmapFactory.decodeStream(is1);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bitmap1);
LinearLayout.LayoutParams myGallery21= new LinearLayout.LayoutParams(100, 100);
myGallery21.setMargins(20, 40, 10, 0);
//its is also working
myGallery.addView(imageView,myGallery1);
}
} catch (IOException e) {
myGallery2.addView(imageView,myGallery21);
}
} catch (IOException e) {
Log.e("GalleryWithHorizontalScrollView", e.getMessage(), e);
}
}
如果我做一個horizontalScrollView和負載imageviews在它那麼它工作正常,但對於第二horizontalScrollView它給我錯誤在線76
Bitmap bitmap1 = BitmapFactory.decodeStream(is1);
和我的日誌貓是這個
05-14 11:13:26.000: E/AndroidRuntime(8350): FATAL EXCEPTION: main
05-14 11:13:26.000: E/AndroidRuntime(8350): java.lang.OutOfMemoryError
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:643)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.example.gallery.MainActivity.onCreate(MainActivity.java:76)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.Activity.performCreate(Activity.java:4470)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.os.Looper.loop(Looper.java:137)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-14 11:13:26.000: E/AndroidRuntime(8350): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 11:13:26.000: E/AndroidRuntime(8350): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-14 11:13:26.000: E/AndroidRuntime(8350): at dalvik.system.NativeStart.main(Native Method)
多少是每個的大小,讓他們圖片?你需要在不使用時回收位圖 – Raghunandan 2013-05-14 06:22:00
嗯,你也可以[看看這個鏈接](http://developer.android.com/training/displaying-bitmaps/index.html) – CRUSADER 2013-05-14 06:36:06
他們共有12張圖片4是1024x780,而其餘的是200x200大小....在哪裏調用bitmap.recycle()....我只是加載圖像2次在actitvity – Passion 2013-05-14 06:38:38