出位图的Andr​​oid记忆 [英] Out of Memory on Bitmap Android

查看:156
本文介绍了出位图的Andr​​oid记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android的实现这个

对于这一点,我必须加载从资源文件夹的图片和我的xml文件做两个 Horizo​​ntalScrollView 和动态加载的ImageView 在里面。对于装载的ImageView我用这code

 的LinearLayout myGallery =(的LinearLayout)findViewById(R.id.mygallery);尝试{
    字符串galleryDirectoryName =画廊;
    的String [] = listImages getAssets()列表(galleryDirectoryName)。
    对于(字符串imageName:listImages){
        InputStream为= getAssets()开(galleryDirectoryName +/+ imageName)。
        位图位图= BitmapFactory.de codeStream(是);        ImageView的ImageView的=新ImageView的(getApplicationContext());
        imageView.setLayoutParams(新ViewGroup.LayoutParams(100,100));        // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageBitmap(位图);        LinearLayout.LayoutParams myGallery1 =新LinearLayout.LayoutParams(100,100);
        myGallery1.setMargins(20,0,10,0);        //它也在努力
        // imageView.setLayoutParams(myGallery1);        imageView.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看视图){
                // diplayImage.setImageBitmap(位图);
            }
        });        Log.e(GalleryWithHorizo​​ntalScrollView,e.getMessage(),E);
    }

//重复上述code第二horizo​​ntalScrollView加载的ImageView

 的LinearLayout myGallery2 =(的LinearLayout)findViewById(R.id.mygallery2);尝试{
    字符串galleryDirectoryName1 =库2;
    的String [] = listImages2 getAssets()列表(galleryDirectoryName1)。
    对于(字符串imageName:listImages2){
        InputStream的IS1 = getAssets()开(galleryDirectoryName1 +/+ imageName)。
        位图bitmap1 = BitmapFactory.de codeStream(IS1);        ImageView的ImageView的=新ImageView的(getApplicationContext());
        imageView.setLayoutParams(新ViewGroup.LayoutParams(100,100));        // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageBitmap(bitmap1);        LinearLayout.LayoutParams myGallery21 =新LinearLayout.LayoutParams(100,100);
        myGallery21.setMargins(20,40,10,0);        //它也在努力        myGallery.addView(ImageView的,myGallery1);
    }
}赶上(IOException异常五){
    myGallery2.addView(ImageView的,myGallery21);
        }
}赶上(IOException异常五){
    Log.e(GalleryWithHorizo​​ntalScrollView,e.getMessage(),E);
}
}

如果我做中已有一个horizo​​ntalScrollView和负载imageviews然后它工作正常,但第二horizo​​ntalScrollView它给我的错误在第76行

 位图bitmap1 = BitmapFactory.de codeStream(IS1);

和我的日志猫是这种

  11月5日至14日:13:26.000:E / AndroidRuntime(8350):致命异常:主要
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):java.lang.OutOfMemoryError
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.graphics.BitmapFactory.nativeDe $ C $检测板(本机方法)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:577)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:643)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在com.example.gallery.MainActivity.onCreate(MainActivity.java:76)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.Activity.performCreate(Activity.java:4470)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.ActivityThread.access $ 600(ActivityThread.java:128)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.os.Handler.dispatchMessage(Handler.java:99)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.os.Looper.loop(Looper.java:137)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在android.app.ActivityThread.main(ActivityThread.java:4517)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在java.lang.reflect.Method.invokeNative(本机方法)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在java.lang.reflect.Method.invoke(Method.java:511)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:993)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
十一月5日至14日:13:26.000:E / AndroidRuntime(8350):在dalvik.system.NativeStart.main(本机方法)


解决方案

您可以找到答案的>,

和你应该考虑最重要的是要优化你的位图的缩略图。
试试这个:

 公共静态位图德codeSampledBitmapFromInput(上下文的背景下,
        InputStream的输入,INT reqWidth,诠释reqHeight){    //首先去code。与inJustDe codeBounds = true来检查尺寸
    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeStream(输入,空,期权);    //计算inSampleSize
    options.inSampleSize = calculateInSampleSize(选项,reqWidth,
            reqHeight);    //德code位与inSampleSize集
    options.inJustDe codeBounds = FALSE;
    返回BitmapFactory.de codeStream(输入,空,期权);
}

下面reqWidth是缩略图的宽度同样为reqHeight。

和对于ListView的尝试实施你的位图缓存,并尝试通过使用一个唯一的密钥可能是文件名(因为你不应该多次保存相同的图像),让他们

I want to make implement this in android For this, I have to load images from asset folder and I am making two HorizontalScrollView in xml file and load dynamically ImageView in it. For loading ImageView I am using this code

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);
    }

//Repeating Above code to load imageView in second horizontalScrollView

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);
}
}

If I make one horizontalScrollView and load imageviews in it then it works fine but for second horizontalScrollView it gives me error at line 76

Bitmap bitmap1 = BitmapFactory.decodeStream(is1);

and my Log cat is this

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)

You can find your answer here,

and most important thing you should consider is to optimize your bitmap for your thumbnail. Try this :

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);
}

Here reqWidth is width of thumbnail and same for reqHeight.

and For listView try to implement a cache for your bitmaps and try to get them by using a unique key may be file name (because you should not save same images multiple times)

这篇关于出位图的Andr​​oid记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆