如何在android wear中加载URL图像?

时间:2023-01-24 16:16:12

Right now I am using Glide-library to load the image in Android Wear. It doesn't load the image in most of the times. However, it loads image sometimes. Don't know what going wrong in my code.

现在我正在使用Glide-library在Android Wear中加载图片。在大多数情况下,它不会加载图像。然而,它有时会加载图像。不知道我的代码出了什么问题。

Note Wear is connected to the device via Bluetooth and I successfully get JSON response of Webservice in Android Wear via Broadcast Receiver from mobile. All data are displayed properly in wear except the images.

Note Wear通过蓝牙连接到设备上,我通过手机的广播接收器成功获得Android Wear的Webservice JSON响应。除图片外,所有数据均显示正确。

Glide.with(mContext)
   .load("http://www.hanamoflorist.ca/images/uploads/Spring5InchesCubeVaseArrangement$45.00.jpg")
      .listener(new RequestListener<String, GlideDrawable>() {

        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            Log.e("exception in image", "" + e);
            Toast.makeText(mContext, "" + e, Toast.LENGTH_LONG).show();
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    }).error(R.drawable.ic_placeholder_image)
        .into(((ItemViewHolder) holder).ivCardImage);

2 个解决方案

#1


2  

I think you should use, DaVinci for image loading in Wearable,

我认为你应该用DaVinci在可穿戴设备上进行图像加载,

DaVinci.with(context).load("Your Url").into(imageView);

Make sure you use the same playservices version as the library,

确保您使用与库相同的playservices版本,

You will be able to integrate the same by adding this to your gradle:

你可以通过将这个添加到你的等级中来整合相同的东西:

wear :

穿:

compile ('com.github.florent37:davinci:1.0.3@aar'){
    transitive = true
}

Mobile:

手机:

compile ('com.github.florent37:davincidaemon:1.0.3@aar'){
     transitive = true
}

Hope you will get what you want.

希望你能得到你想要的。

#2


0  

The issue is due to socket time out...

问题是由于插座超时了……

You can resolve it using Glide itself. You just need to use Glide with OKHttp3, and set Timeout Limit for OkHttpClient.

你可以用滑翔来解决它。您只需使用Glide和OKHttp3一起使用Glide,并为OkHttpClient设置超时限制。

In your module dependency

在你的模块的依赖

compile 'com.github.bumptech.glide:glide:3.7.0'
compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){
    exclude group: 'glide-parent'
}

Customize glide settings

定制滑翔的设置

public class MyGlideModule implements GlideModule {
   @Override
   public void applyOptions(Context context, GlideBuilder builder) {

   }

   @Override
   public void registerComponents(Context context, Glide glide) {

       OkHttpClient.Builder builder = new OkHttpClient.Builder();

       // set your timeout here
       builder.readTimeout(30, TimeUnit.SECONDS);
       builder.writeTimeout(30, TimeUnit.SECONDS);
       builder.connectTimeout(30, TimeUnit.SECONDS);
       OkHttpUrlLoader.Factory factory = new    OkHttpUrlLoader.Factory(client);
       glide.register(GlideUrl.class, InputStream.class, factory);
   }
}

In manifest put below code

显示在代码下面。

 <meta-data
        android:name="YourPath.MyGlideModule"
        android:value="GlideModule" />

#1


2  

I think you should use, DaVinci for image loading in Wearable,

我认为你应该用DaVinci在可穿戴设备上进行图像加载,

DaVinci.with(context).load("Your Url").into(imageView);

Make sure you use the same playservices version as the library,

确保您使用与库相同的playservices版本,

You will be able to integrate the same by adding this to your gradle:

你可以通过将这个添加到你的等级中来整合相同的东西:

wear :

穿:

compile ('com.github.florent37:davinci:1.0.3@aar'){
    transitive = true
}

Mobile:

手机:

compile ('com.github.florent37:davincidaemon:1.0.3@aar'){
     transitive = true
}

Hope you will get what you want.

希望你能得到你想要的。

#2


0  

The issue is due to socket time out...

问题是由于插座超时了……

You can resolve it using Glide itself. You just need to use Glide with OKHttp3, and set Timeout Limit for OkHttpClient.

你可以用滑翔来解决它。您只需使用Glide和OKHttp3一起使用Glide,并为OkHttpClient设置超时限制。

In your module dependency

在你的模块的依赖

compile 'com.github.bumptech.glide:glide:3.7.0'
compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){
    exclude group: 'glide-parent'
}

Customize glide settings

定制滑翔的设置

public class MyGlideModule implements GlideModule {
   @Override
   public void applyOptions(Context context, GlideBuilder builder) {

   }

   @Override
   public void registerComponents(Context context, Glide glide) {

       OkHttpClient.Builder builder = new OkHttpClient.Builder();

       // set your timeout here
       builder.readTimeout(30, TimeUnit.SECONDS);
       builder.writeTimeout(30, TimeUnit.SECONDS);
       builder.connectTimeout(30, TimeUnit.SECONDS);
       OkHttpUrlLoader.Factory factory = new    OkHttpUrlLoader.Factory(client);
       glide.register(GlideUrl.class, InputStream.class, factory);
   }
}

In manifest put below code

显示在代码下面。

 <meta-data
        android:name="YourPath.MyGlideModule"
        android:value="GlideModule" />