青春须早为,岂能长少年。这篇文章主要讲述如何从android中重定向url加载图像相关的知识,希望能为你提供帮助。
嗨,我正面临这个问题
我从RESTCall获取了一个URL
网址是http://hck.re/kWWxUI
但是当我在浏览器中检查时,它会重定向到https://s3-ap-southeast-1.amazonaws.com/he-public-data/afreen2ac5a33.jpg
如何将此图像加载到我的imageView中我已经知道如何将毕加索的图像加载到imageView中。任何帮助,将不胜感激
编辑:我已添加此代码
OkHttpClient client = new OkHttpClient();
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build();
picasso.load(music.getUrl())
.fit()
.into(holder.cover_image);
没有运气
答案custom Picasso.java
import android.content.Context;
import android.util.Log;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
/**
* Created by Hrishikesh Kadam on 19/12/2017
*/public class CustomPicasso {private static String LOG_TAG = CustomPicasso.class.getSimpleName();
private static boolean hasCustomPicassoSingletonInstanceSet;
public static Picasso with(Context context) {if (hasCustomPicassoSingletonInstanceSet)
return Picasso.with(context);
try {
Picasso.setSingletonInstance(null);
} catch (IllegalStateException e) {
Log.w(LOG_TAG, "->
Default singleton instance already present" +
" so CustomPicasso singleton cannot be set. Use CustomPicasso.getNewInstance() now.");
return Picasso.with(context);
}Picasso picasso = new Picasso.Builder(context).
downloader(new OkHttp3Downloader(context))
.build();
Picasso.setSingletonInstance(picasso);
Log.w(LOG_TAG, "->
CustomPicasso singleton set to Picasso singleton." +
" In case if you need Picasso singleton in future then use Picasso.Builder()");
hasCustomPicassoSingletonInstanceSet = true;
return picasso;
}public static Picasso getNewInstance(Context context) {Log.w(LOG_TAG, "->
Do not forget to call customPicasso.shutdown()" +
" to avoid memory leak");
return new Picasso.Builder(context).
downloader(new OkHttp3Downloader(context))
.build();
}
}
build.gradle(模块:app)
android {...}dependencies {...compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
}
用法 -
CustomPicasso.with(context)
.load("http://hck.re/kWWxUI")
.into(imageView);
【如何从android中重定向url加载图像】有关最新版本,请查看CustomPicasso gist-https://gist.github.com/hrishikesh-kadam/09cef31c736de088313f1a102f5ed3a3
推荐阅读
- Android - OnTouchListener如果已按下屏幕则不会触发
- Android(无法从mysql数据库中检索图像URL并将其显示在imageView中)
- ImageView在android中不起作用
- 如何在android中使用SwipeRefreshLayout和imageView进入NestedScrollView
- 移动ImageView(Android)[重复]
- 带有setAdjustViewBounds的Android ImageView在ConstraintLayout中具有无法解释的行为
- 在android [copy]中没有设置从相机到imageview的图像
- 5+ Web项目的最佳免费jQuery和JavaScript动态甘特图
- 最佳5(最佳图像颜色提取JavaScript和jQuery插件)