【问题标题】:java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)java.lang.OutOfMemoryError 在 android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)
【发布时间】:2014-07-30 19:39:45
【问题描述】:

我正在尝试在单击按钮时执行动画.. 步骤如下 单击按钮时..

  1. 按钮图像发生变化
  2. 动画播放
  3. 显示下一个布局..

但是我遇到了内存不足的异常..

当动画文件未添加到项目中时,没有错误。但是自从加入了动画之后,问题就来了。

我在这里使用了 3 个类文件(home_screen、button_anime 和 home)

home_screen.java 接收按钮点击信息,更改按钮图像并传递给 button_anime 类,动画文件在 button_anime.java 中启动,动画播放后从 home.java 显示下一个布局

日志猫如下..

 E/AndroidRuntime(1255): java.lang.OutOfMemoryError
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
 E/AndroidRuntime(1255):    at android.content.res.Resources.loadDrawable(Resources.java:2110)
 E/AndroidRuntime(1255):    at android.content.res.Resources.getDrawable(Resources.java:700)
 E/AndroidRuntime(1255):    at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
 E/AndroidRuntime(1255):    at android.content.res.Resources.loadDrawable(Resources.java:2092)
 E/AndroidRuntime(1255):    at android.content.res.Resources.getDrawable(Resources.java:700)
 E/AndroidRuntime(1255):    at android.view.View.setBackgroundResource(View.java:15303)
 E/AndroidRuntime(1255):    at com.quinoid.thomasinternational.Button_Anime.onCreate(Button_Anime.java:19)
 E/AndroidRuntime(1255):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(1255):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1255):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1255):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1255):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1255):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1255):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1255):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1255):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1255):    at dalvik.system.NativeStart.main(Native Method)

我的 home_screen.java

home.setOnClickListener(new OnClickListener() { <-- error happens somewhere here

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1);
        Drawable d = new BitmapDrawable(getResources(),b); 
        home.setBackgroundDrawable(d); <-- this dose not work..
        Intent myIntent = new Intent(Home_Screen.this, Button_Anime.class);
        startActivity(myIntent);
    }
});

【问题讨论】:

  • 您的内存不足。可能是位图太大,也可能是你在其他地方有泄漏。您需要在堆分析器中查看您的内存使用情况,以了解发生了什么。
  • 我所有的图像都是 30kb 和 50kb .. 那么我如何检查我的堆分析器? @GabeSechan
  • Eclipse 内置了一个。您应该寻找一些没有意义的类或大量分配的失控数字。
  • 两者都做了..没用..@prakash

标签: java android animation out-of-memory


【解决方案1】:

_img 是你的图像视图。你必须解码你的图像并设置它的大小,就像我正在做的那样。

 File imgFile = new File(_path); // path of your file
    if (imgFile.exists()) {
        FileInputStream fis = new FileInputStream(imgFile);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inPurgeable = true;
        options.inScaled = true;
        Bitmap bm = BitmapFactory.decodeStream(fis, null,options);
       _img.setImageBitmap(bm);

【讨论】:

  • 投了赞成票。这似乎与我的问题有关,但我认为仍然存在内存泄漏
  • 非常好。坚持下去
【解决方案2】:

您应该使用decodeResource(Resources res, int id, BitmapFactory.Options opts),并指定inSampleSize -

如果设置为大于 1 的值,则请求解码器对原始图像进行二次采样 图像,返回较小的图像以节省内存。

【讨论】:

  • 我应该在哪里使用这个??你能举个例子吗? SampleSize 值也是 kb 的大小吗?或尺寸?? @yushulx
  • Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1, opts);例如inSampleSize == 4 返回的图片是原图宽/高的1/4 , 和 1/16 的像素数。
猜你喜欢
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多