ImageView网络图片无法加载
Glide 报错“Load failed for xxxx”
1.可能是你给的图片地址的问题,把http改成https(我的蕾姆就是http改https就行了!)图片后缀可以是jpg,png或者webp
2.需要给网络权限
3.google()要删去
具体看下面代码
代码
activity_image_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_1"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#1199AA"
android:src="@drawable/bg_iron_man"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/iv_2"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#1199AA"
android:src="@drawable/bg_iron_man"
android:scaleType="fitCenter"
android:layout_below="@id/iv_1"
android:layout_marginTop="10dp"/>
<ImageView
android:id="@+id/iv_3"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#1199AA"
android:src="@drawable/bg_iron_man"
android:scaleType="centerCrop"
android:layout_below="@id/iv_2"
android:layout_marginTop="10dp"/>
<ImageView
android:id="@+id/iv_4"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#1199AA"
android:layout_below="@id/iv_3"
android:layout_marginTop="10dp"/>
<!--background是背景,src是内容,都可以设为图片
scaleType是缩放的类型:
fitXY-图片撑满控件,会拉伸
fitCenter 保持宽高比缩放,直至完全展示
centerCrop 保持宽高比缩放,直至完全覆盖控件,裁剪显示
-->
</RelativeLayout>
build.gradle
在这个地方把GitHub上的glide相关代码复制过来
注意google()我们用不了,要删去。
### build.gradle
在这个地方把GitHub上的glide相关代码复制过来
> apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
//把Google()删掉,因为我们访问不了谷歌
}
//把Github上的代码复制过来
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
ImageViewActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageViewActivity extends AppCompatActivity {
private ImageView mIv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
mIv4=findViewById(R.id.iv_4);
Glide.with(this).load("https://pic1.zhimg.com/v2-bb94fd1e01ac7698f84cfa10f242765b_xl.jpg").into(mIv4);
}
}
Androidmainfest.xml
这里要给网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<uses-permission android:name="android.permission.INTERNET"/>
<!--申明权限,网络请求 -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".ImageViewActivity" >
</activity>
<activity android:name=".CheckBoxActivity" />
<activity android:name=".RadioButtonActivity" />
<activity android:name=".EditTextActivity" />
<activity android:name=".ButtonActivity" />
<activity android:name=".TextViewActivity" />
<activity android:name=".Listview.ListViewActivity" />
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>