有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java异常无法从资源中找到可绘制的图标

背景

大家好。我正在制作一个应用程序,它有一个带有自定义对象列表的主活动。每个项目显示一张图片、一个标题和一个副标题,所有这些都取自项目的对象属性

在应用程序的第一个版本中,当单击某个项目时,该对象以Parcelable的形式发送给另一个活动,该活动显示了该对象的所有属性,包括图像。当时,图像的属性只是图像的ResourcesID,因此我只需加载具有该ID的图像

在我的当前版本中,我添加了一个新活动,允许用户设置对象信息,以便将新对象添加到主活动列表中。但是,用户从库中选择图像。为了解决这个问题,我将initial ID属性改为Drawable,这样我就可以直接分配Resources图片和画廊挑选的图片
这给了我一个FAILED BINDER TRANSACTION错误,所以我决定将图像保存为byte[]以避免出现这种情况

现状和问题

我的自定义对象构造函数获取Drawable,并使用以下方法将其转换为byte[]

private byte[] drawableToBytes(Drawable img)
{
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    ((BitmapDrawable)img).getBitmap().compress(Bitmap.CompressFormat.PNG, 50, stream);

    return stream.toByteArray();
}

为了从Parcelable获取对象,我使用以下方法:

public CentroMando(Parcel in)
{
    String[] datos = new String[11];

    in.readStringArray(datos);

    this.setAbreviatura(datos[0]);
    this.setNombre(datos[1]);
    this.setImagenBytes(Base64.decode(datos[2], Base64.DEFAULT));   // <----- Image
    this.setLugar(new Lugar(datos[3],
                            Double.parseDouble(datos[4]),
                            Double.parseDouble(datos[5]),
                            Double.parseDouble(datos[6])));
    this.setComandante(datos[7]);
    this.setEnlaceCentroMando(datos[8]);
    this.setEnlaceLocalizacion(datos[9]);
    this.setEnlaceComandante(datos[10]);
}

要将对象转换为Parcelable,请执行以下操作:

public void writeToParcel(Parcel dest, int flags)
{
    dest.writeStringArray(new String[]
    {
        this.getAbreviatura(),
        this.getNombre(),
        Base64.encodeToString(this.getImagenBytes(), Base64.DEFAULT),   // <----- Image
        this.getLugar().getNombre(),
        String.valueOf(this.getLugar().getLatitud()),
        String.valueOf(this.getLugar().getLongitud()),
        String.valueOf(this.getLugar().getDireccion()),
        this.getComandante(),
        this.getEnlaceCentroMando(),
        this.getEnlaceLocalizacion(),
        this.getEnlaceComandante()
    });
}

要从byte[]获取Drawable,我使用以下命令:

public Drawable getImagenBytesDrawable()
{
    byte[] datosBitmap = this.getImagenBytes();

    Bitmap imagenBitmap = BitmapFactory.decodeByteArray(datosBitmap, 0, datosBitmap.length);

    return new BitmapDrawable(imagenBitmap);
}

现在,当我点击ListView中的项目时,一些加载没有任何问题,并且显示图像和属性很好,但是其他人在Logcat上没有任何错误地关闭应用程序。我必须删除Logcat中的过滤器才能真正找到问题:

2019-11-14 01:33:18.287 2110-14522/? E/ActivityManager: Transaction too large, intent: Intent { cmp=com.example.ud4_propuesto_1/.DatosCentroMandoActivity (has extras) }, extras size: 594808, icicle size: 0
2019-11-14 01:33:18.287 2110-14522/? D/GamePkgDataHelper: notifyAppCreate(), pkgName: com.example.ud4_propuesto_1, sendRet: true
2019-11-14 01:33:18.287 2110-12153/? D/GamePkgDataHelper: getGamePkgData(). com.example.ud4_propuesto_1
2019-11-14 01:33:18.287 2110-12153/? D/GameManagerService: handleMessage(), MSG_APP_CREATE. ignore. pkgName: com.example.ud4_propuesto_1
2019-11-14 01:33:18.289 2110-14522/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 598680)
2019-11-14 01:33:18.289 2110-14522/? E/ActivityManager: Second failure launching com.example.ud4_propuesto_1/.DatosCentroMandoActivity, giving up
    安卓.os.TransactionTooLargeException: data parcel size 598680 bytes
        at 安卓.os.BinderProxy.transactNative(Native Method)
        at 安卓.os.BinderProxy.transact(Binder.java:1145)
        at 安卓.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1897)
        at 安卓.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
        at com.安卓.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
        at com.安卓.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1862)
        at com.安卓.server.am.ActivityStackSupervisor.attachApplicationLocked(ActivityStackSupervisor.java:1199)
        at com.安卓.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:10029)
        at com.安卓.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:10097)
        at 安卓.app.IActivityManager$Stub.onTransact(IActivityManager.java:170)
        at com.安卓.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:4162)
        at 安卓.os.Binder.execTransact(Binder.java:739)
2019-11-14 01:33:18.290 2110-14522/? I/ActivityManager: Process com.example.ud4_propuesto_1 (pid 25813) has died: fore TOP (214,2655)

传统信息

我将Drawable图像保存为xxxhdpi
使应用程序崩溃的项目图像的重量分别为410 KB、393 KB和393 KB。其余图像的重量小于345KB
我测试过,只有那些图像较重的项目才会导致应用程序崩溃

问题

有什么办法解决这个问题吗


共 (1) 个答案

  1. # 1 楼答案

    Intent的总容量限制为1MB,因此在其中发送位图是行不通的

    您应该让您的数据对象接受从预设(可绘制分辨率)或从图库(缓存文件的url?)中拾取的图像,然后解析它并相应地加载图像