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