首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:内存错误和位图的大小/质量

问Android:内存错误和位图的大小/质量
EN

Stack Overflow用户
提问于 2013-02-19 19:03:13
回答 2查看 197关注 0票数 0

我在同一活动中启动了2个AnimationDrawables,但出现了内存错误。这是相当令人惊讶的,因为我减少了图像的大小(宽度=屏幕的宽度),我不能再减少它了,因为质量会降低。例如,如果我使用width = width of the screen/2,它可以工作,但质量很差

在logcat中,它被写成

代码语言:javascript
复制
02-19 10:56:44.769: E/dalvikvm-heap(3118): 383916-byte external allocation too large for this process.
02-19 10:56:44.809: E/GraphicsJNI(3118): VM won't let us allocate 383916 bytes
02-19 10:56:44.809: W/dalvikvm(3118): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-19 10:56:44.819: E/AndroidRuntime(3118): FATAL EXCEPTION: main
02-19 10:56:44.819: E/AndroidRuntime(3118): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

我不认为我有一个内存泄漏,但检查这篇文章http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html我试图检测一个潜在的内存泄漏,但对我来说似乎没有。

我真的需要一些帮助,因为这不是我在这个项目中的第一个内存错误,我总是努力处理它。

下面是我的代码:

代码语言:javascript
复制
private void startAnimation(AnimationDrawable animation1,AnimationDrawable animation2){
    int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10down2,R.drawable.fire11down2,R.drawable.fire12down2,R.drawable.fire13down2,R.drawable.fire14down2,R.drawable.fire15down2,R.drawable.fire16down2};
    int drawables2[] = new int[] {R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire10up,R.drawable.fire11up,R.drawable.fire12up,R.drawable.fire13up,R.drawable.fire14up,R.drawable.fire15up,R.drawable.fire16up};
    bitmapDrawable = new BitmapDrawable[drawables.length];
    bitmapDrawable2 = new BitmapDrawable[drawables2.length];
    for (int i=0;i<drawables.length;i++) {
        bitmapDrawable[i]=new BitmapDrawable(decodeSampledBitmapFromResource(getResources(), drawables[i],width));
        Log.w("BITMAP","i="+i);
        bitmapDrawable2[i]=new BitmapDrawable(decodeSampledBitmapFromResource(getResources(), drawables2[i],width));
        animation1.addFrame(bitmapDrawable[i],50);
        animation2.addFrame(bitmapDrawable2[i],50);
    }
    animation1.setOneShot(true);
    animation2.setOneShot(true);
  image2.setLayoutParams(params);
        image2.setImageDrawable(animation1);
        image2.post(new Starter(animation1));
        image3.setLayoutParams(params);
        image3.setImageDrawable(animation2);
        image3.post(new Starter(animation2));

}
      class Starter implements Runnable {
          AnimationDrawable animation = new AnimationDrawable();
          public Starter(AnimationDrawable animation) {
              this.animation = animation;
          }

          public void run() {
              animation.start();
          }
       }

如何减轻位图的权重(如果有必要?该问题可能在某些地方else_,并保持图像的质量。我看不出有什么问题,我只用了30张图片,其他的应用程序用的更多!

EN

回答 2

Stack Overflow用户

发布于 2013-02-19 19:56:14

你可以从你的文件中创建ScaledBitmap来减小EX的大小:-

代码语言:javascript
复制
Bitmap scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, 600, 600,
                        false);

这将使其大小减小到600x600,并减小其大小。你可以改变你喜欢的像素。

票数 0
EN

Stack Overflow用户

发布于 2013-02-19 21:14:53

在这里,您将重新调整质量大小。

代码语言:javascript
复制
   try {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;


             bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                    imageFileUri), null, bmpFactoryOptions);


            bmpFactoryOptions.inSampleSize = 2;
            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                    imageFileUri), null, bmpFactoryOptions);

在这里,我使用这个方法从sdcard中获取图像。然后显示出来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14955851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档