0

內存不足我想在安卓 enter image description here 對於此實現這一點,我必須從資源文件夾載入圖像,我在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); 
    } 

//重複上述代碼在第二horizo​​ntalScrollView加載的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); 
} 
} 

如果我做一個horizo​​ntalScrollView和負載imageviews在它那麼它工作正常,但對於第二horizo​​ntalScrollView它給我錯誤在線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) 
+0

多少是每個的大小,讓他們圖片?你需要在不使用時回收位圖 – Raghunandan 2013-05-14 06:22:00

+0

嗯,你也可以[看看這個鏈接](http://developer.android.com/training/displaying-bitmaps/index.html) – CRUSADER 2013-05-14 06:36:06

+0

他們共有12張圖片4是1024x780,而其餘的是200x200大小....在哪裏調用bitmap.recycle()....我只是加載圖像2次在actitvity – Passion 2013-05-14 06:38:38

回答

1

你可以找到你的答案here

,最重要的事情,你應該考慮的是優化你的位圖的縮略圖。 嘗試此:

public static Bitmap decodeSampledBitmapFromInput(Context context, 
     InputStream input, int reqWidth, int reqHeight) { 

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

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeStream(input, null, options); 
} 

這裏reqWidth是縮略圖的寬度和相同的用於reqHeight。

和對於ListView的嘗試實施你的位圖緩存,並嘗試通過使用一個唯一的密鑰可能是文件名(因爲你不應該多次保存相同的圖像)

+0

嗯,你也可以[查看這個鏈接](http://developer.android.com/training/displaying-bitmaps/index。html) – CRUSADER 2013-05-14 06:35:41

+0

您還可以添加 options.inPurgeable = true; options.inInputShareable = true; – 2013-05-14 06:42:10

0

簡單,不負載太多圖片...

使用一些緩存像LRUCache有固定的大小。如果圖像丟失,您必須先加載它們。要提高性能並修復OOME

http://developer.android.com/reference/android/util/LruCache.html

+0

谷歌播放商店有很多圖像。你可以向下滾動列表。 「簡單,不加載太多圖像」不是答案。在這種情況下,延遲加載或UIL應該有所幫助 – Raghunandan 2013-05-14 06:31:18

+0

他們管理內存aswel,所以它們不會全部加載一次。那就是LRU來玩 – Kitesurfer 2013-05-14 06:32:55

0

你需要使用BitmapFactory.Options

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize =8; 

然後

Bitmap bitmap1 = BitmapFactory.decodeStream(is1,null,options); 

,以便只有所需尺寸的位圖是加載訪問圖片進入內存

更多看到Loading Large Bitmaps Efficiently