在wear Service中调用blockingConnect时出现IllegalStateException

a.p*_*.p. 3 android assets illegalstateexception wear-os

我正在尝试开发Android Wear应用程序并从移动助手活动中传递资产.我已经按照官方文档传递资产 - 转移资产,但是当我尝试从onDataChanged函数中传递的资产加载位图时,我收到以下错误.请注意,代码在传递String值时有效.代码如下:

public Bitmap loadBitmapFromAsset(Bitmap bitmap, Asset asset) {
    if (asset == null) {
        throw new IllegalArgumentException("Asset must be non-null");
    }

    ConnectionResult result = mGoogleApiClient.blockingConnect(5000, TimeUnit.MILLISECONDS);
    if (!result.isSuccess()) {
        return null;
    }

    InputStream assetInputStream = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await().getInputStream();
    mGoogleApiClient.disconnect();

    if (assetInputStream == null) {
        return null;
    }

    if (bitmap != null) {
        bitmap.recycle();
        bitmap = null;
    }

    bitmap = BitmapFactory.decodeStream(assetInputStream);
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

java.lang.IllegalStateException:不得在com.google.android.gms.com上发送com.google.android.gms.common.internal.zzx.zza(未知来源)的UI线程上调用blockingConnect. zzj.blockingConnect(未知来源)......

关于是什么导致这个的任何想法?

a.p*_*.p. 5

最终解决方案是在runnable中执行loadBitmapFromAsset.不明白为什么我之前没有想到这个...我没有找到类似的帖子,所以希望它会帮助别人.

new Thread(new Runnable() {
    @Override
    public void run() {
    ...
    }
}).start();
Run Code Online (Sandbox Code Playgroud)