Android上内存不足
我想让它在android中实现 为此,我必须从asset文件夹加载图像,并在xml文件中创建两个Android上内存不足,android,memory,memory-management,memory-leaks,out-of-memory,Android,Memory,Memory Management,Memory Leaks,Out Of Memory,我想让它在android中实现 为此,我必须从asset文件夹加载图像,并在xml文件中创建两个HorizontalScrollView,然后在其中动态加载ImageView。对于加载ImageView,我使用以下代码 LinearLayout myGallery = (LinearLayout) findViewById(R.id.mygallery); try { String galleryDirectoryName = "gallery"; String[] listI
HorizontalScrollView
,然后在其中动态加载ImageView
。对于加载ImageView,我使用以下代码
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);
}
//重复上述代码以在第二个水平滚动视图中加载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并在其中加载ImageView,那么它可以正常工作,但对于第二个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)
简单,不要加载太多的图像 使用一些缓存,比如固定大小的LRUCache。如果图像丢失,您必须首先加载它们。应该提高性能并修复您的OOME
您需要使用BitmapFactory.Options访问图像 然后 这样,只需将所需大小的位图加载到内存中 有关更多信息,请参见您可以找到您的答案 最重要的是你应该为你的缩略图优化位图。 试试这个:
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,尝试为位图实现缓存,并尝试使用唯一的键获取位图,该键可能是文件名(因为不应多次保存相同的图像)每个图像的大小是多少?不使用时,您需要回收位图。嗯,您还可以在其中总共有12幅图像,其中4幅为1024x780,其余为200x200大小。。。。在哪里调用bitmap.recycle()…我只是在Activity中加载了两次图像。alonegoogle play store有很多图像。您可以向下滚动列表。“简单,不要加载太多图像”不是答案。在这种情况下,延迟加载或UIL应该有助于他们管理内存,这样他们就不会一次全部加载。这就是LRU发挥作用的地方。嗯,你也可以添加选项。inpurgable=true;options.inInputShareable=true;
Bitmap bitmap1 = BitmapFactory.decodeStream(is1,null,options);
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);
}