How to load URL image in android wear?

一世执手 提交于 2019-12-10 01:57:08

问题


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.

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.

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);

回答1:


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

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

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

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:


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.

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" />


来源:https://stackoverflow.com/questions/41499249/how-to-load-url-image-in-android-wear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!