先使用OkHttp把图片下载下来,再使用Glide显示图片。效果展示在最后面
在Moudle的gradle中添加
implementation("com.squareup.okhttp3:okhttp:4.2.0")
如果你用的是http访问网络,会报错。现在Android P全面禁止了非https链接(参考文章)
但是国内的很多网站都是非https的,怎么办呢?需要使用xml文件设置属性禁用掉这一设置,在res文件夹下新建目录xml,然后创建文件network_security_config.xml(文件名随意):
然后在AndroidManifest.xml文件的Application标签添加属性:
问题解决
下载相关代码:
private void download() {
String url="https://sf6-ttcdn-tos.pstatp.com/img/ee-finolhu/034e2e9d3cfe49f8bb0a3367c9afec47~noop.image";
//1.创建OkHttpClient对象
OkHttpClient client=new OkHttpClient();
//2.创建请求对象Request
Request request=new Request.Builder()
.url(url)
.build();
//3.执行请求
Call call = client.newCall(request);
//同步请求
//Response response = call.execute();
//执行异步请求的方式
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("onResponse", "onFailure: ");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
if (response.body().contentLength()>0){
InputStream is=response.body().byteStream();
File file=new File(Environment.getExternalStorageDirectory()+File.separator+BuildConfig.APPLICATION_ID+File.separator+"images");
Log.d("OkHttpTest", "onResponse: "+file.toString());
if (file.getParentFile().exists()){
try {
FileOutputStream fos=new FileOutputStream(file);
int len;
while ((len=is.read())!=-1){
fos.write(len);
}
} catch (FileNotFoundException e) {
Log.d("OkHttpTest", "FileNotFoundException: 异常了");
e.printStackTrace();
} catch (IOException e) {
Log.d("OkHttpTest", "IOException:异常了 ");
e.printStackTrace();
}
}else {
file.getParentFile().mkdirs();
}
}else {
Toast.makeText(MainActivity.this, "访问的文件不存在", Toast.LENGTH_SHORT).show();
}
}
});
}
这个url是一张图片的网址,下载完成后在打印出来的文件路径下可以看到。
先看看图长啥样:传送门
在Moudle的gradle中添加:
implementation 'com.github.bumptech.glide:glide:4.10.0'
Glide.with(context).load(url).into(ImageView);
File file=new File(Environment.getExternalStorageDirectory()+File.separator+BuildConfig.APPLICATION_ID+File.separator+"images");
Glide.with(context).load(file).into(ImageView);
int resourceId = R.mipmap.ic_launcher;
Glide.with(context).load(resourceId).into(ImageView);
String url = "xxxxx";
Glide.with( context ).load( url ).into( ImageView);
Glide.with( context ).load(url ).placeholder( R.drawable.xxx).error( R.drawable.xxx).into( ImageView);
加权限
布局文件
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_download;
Button btn_show;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_download=findViewById(R.id.button_download);
btn_show=findViewById(R.id.button_show);
iv=findViewById(R.id.iv);
btn_show.setOnClickListener(this);
btn_download.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_download:
Log.d("onResponse", "onClick: 我单击了");
download();
break;
case R.id.button_show:
show();
break;
}
}
private void show() {
File file=new File(Environment.getExternalStorageDirectory()+File.separator+BuildConfig.APPLICATION_ID+File.separator+"images");
Glide.with(MainActivity.this).load(file).into(iv);
}
private void download() {
String url="https://sf6-ttcdn-tos.pstatp.com/img/ee-finolhu/034e2e9d3cfe49f8bb0a3367c9afec47~noop.image";
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("onResponse", "onFailure: ");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
if (response.body().contentLength()>0){
InputStream is=response.body().byteStream();
File file=new File(Environment.getExternalStorageDirectory()+File.separator+BuildConfig.APPLICATION_ID+File.separator+"images");
Log.d("OkHttpTest", "onResponse: "+file.toString());
if (file.getParentFile().exists()){
try {
FileOutputStream fos=new FileOutputStream(file);
int len;
while ((len=is.read())!=-1){
fos.write(len);
}
} catch (FileNotFoundException e) {
Log.d("OkHttpTest", "FileNotFoundException: 异常了");
e.printStackTrace();
} catch (IOException e) {
Log.d("OkHttpTest", "IOException:异常了 ");
e.printStackTrace();
}
}else {
file.getParentFile().mkdirs();
}
}else {
Toast.makeText(MainActivity.this, "访问的文件不存在", Toast.LENGTH_SHORT).show();
}
}
});
}
}
显示图片直接使用Glide就行了。没必要先下载。
修改上面的show()方法
private void show() {
String url="https://sf6-ttcdn-tos.pstatp.com/img/ee-finolhu/034e2e9d3cfe49f8bb0a3367c9afec47~noop.image";
Glide.with(MainActivity.this).load(url).into(iv);
}
可以直接显示图片。