相关文章:
上面链接先解决一个冲突问题。
1、依赖
// glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
2、 android:usesCleartextTraffic="true"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestDemo1"
tools:targetApi="m">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:usesCleartextTraffic 指示应用程序是否打算使用明文网络流量,例如明文HTTP。目标API级别为27或更低的应用程序的默认值为“ true”。面向API级别28或更高级别的应用默认为“ false”。
当属性设置为“ false”时,平台组件(例如,HTTP和FTP堆栈,DownloadManager和MediaPlayer)将拒绝应用程序使用明文流量的请求。强烈建议第三方库也采用此设置。避免明文通信的主要原因是缺乏机密性,真实性和防篡改保护;网络攻击者可以窃听所传输的数据,并且还可以对其进行修改而不会被检测到。
3、activity
public class MainActivity extends AppCompatActivity {
private ImageView image;
private ImageView ivGlide;
// 网络图片的链接地址
private final static String PATH = "http://bpic.588ku.com/element_origin_min_pic/16/10/29/2ac8e99273bc079e40a8dc079ca11b1f.jpg";
private final static String PATH2 = "http://bpic.588ku.com/element_origin_min_pic/17/06/13/5c5a1442f0ec72e59829ee10d891f224.jpg%21r650";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = findViewById(R.id.image);
ivGlide = findViewById(R.id.iv_glide);
Glide.with(this).load(R.mipmap.scenery1).into(image);
Glide.with(this).load(PATH2).into(ivGlide);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="downloadImageAction"
android:text="传统方式下载图片功能" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rxJavaDownloadImageAction"
android:text="RxJava方式下载图片功能" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ImageView
android:id="@+id/iv_glide"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>