当前位置: 代码迷 >> Android >> Android 4.3中的android.graphics.BitmapFactory.nativeDecodeAsset问题
  详细解决方案

Android 4.3中的android.graphics.BitmapFactory.nativeDecodeAsset问题

热度:59   发布时间:2023-08-04 12:43:33.0

我的应用程序只有Android 4.3出现问题。 我不确定是什么原因导致的,错误是从哪里来的。 基本上,我没有使用任何可以调试的位图函数。 这是来自Google Play的错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capripio.wordzcatch/com.capripio.wordzcatch.SplashActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
            at android.app.ActivityThread.access$700(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)
            Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
            at android.view.LayoutInflater.createView(LayoutInflater.java:626)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
            at android.app.Activity.setContentView(Activity.java:1956)
            at com.capripio.wordzcatch.SplashActivity.onCreate(SplashActivity.java:14)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            ... 11 more
            Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            ... 23 more
            Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
            at android.content.res.Resources.loadDrawable(Resources.java:2988)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
            at android.view.View.<init>(View.java:3563)
            at android.view.View.<init>(View.java:3492)
            at android.view.ViewGroup.<init>(ViewGroup.java:469)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
            ... 26 more

谢谢!

我已经找到了做到这一点并使小班学习的方法。

public abstract class BitmapResLoader {

public static Bitmap decodeBitmapFromResource(Bitmap.Config config, Resources res, int resId, int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = config;

    return BitmapFactory.decodeResource(res, resId, options);
}

private static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
}

并用作

decodeBitmapFromResource(
            Bitmap.Config.ARGB_8888, splashActivity.getResources(),
            R.drawable.bg, 480, 800);

尝试把:

清单应用程序标记中的android:largeHeap =“ true”

如果你看看这条线

Caused by: java.lang.OutOfMemoryError

表示错误是由java.lang.OutOfMemoryError引起的

  相关解决方案