使用Glide将URL下载到ImageView
中非常容易:
Glide
.with(context)
.load(getIntent().getData())
.placeholder(R.drawable.ic_loading)
.centerCrop()
.into(imageView);
我想知道是否可以将其下载到Bitmap
中? 我想下载到一个原始位图中,然后可以使用其他工具进行操作。 我已经查看了代码,但没有看到如何实现。
使用Glide将URL下载到ImageView
中非常容易:
Glide
.with(context)
.load(getIntent().getData())
.placeholder(R.drawable.ic_loading)
.centerCrop()
.into(imageView);
我想知道是否可以将其下载到Bitmap
中? 我想下载到一个原始位图中,然后可以使用其他工具进行操作。 我已经查看了代码,但没有看到如何实现。
这个示例使用Glide将图片下载到位图中。
步骤1 - 在build.gradle中的Module: app中添加以下依赖项:
implementation 'com.github.bumptech.glide:glide:4.9.0'
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Glide.with(this).asBitmap().load("https://www.google.es/images/srpr/logo11w.png").into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
}
第四步 - 将以下代码添加到androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行您的应用程序。
我不得不添加 try-catch 块,因为如果 URL 无效,现在 Glide 会导致应用程序崩溃。
return try {
Glide.with(context)
.asBitmap()
.load(imageURL)
.listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Bitmap>?,
isFirstResource: Boolean
): Boolean {
Log.e(
TAG,
"Texture from ResourceID $resourceId could not be Loaded. " +
"Using default Texture"
)
return false
}
override fun onResourceReady(
resource: Bitmap?,
model: Any?,
target: Target<Bitmap>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
return false
}
})
.placeholder(DEFAULT_IMAGE)
.error(DEFAULT_IMAGE)
.submit()
.get()
} catch (ex: Exception) {
return fromResource(DEFAULT_IMAGE)
}
新版本:
GlideApp.with(imageView)
.asBitmap()
.override(200, 200)
.centerCrop()
.load(mUrl)
.error(R.drawable.defaultavatar)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(System.currentTimeMillis() / (1000*60*60*24))) //refresh avatar cache every day
.into(object : CustomTarget<Bitmap>(){
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onLoadFailed(errorDrawable: Drawable?) {
//add context null check in case the user left the fragment when the callback returns
context?.let { imageView.addImage(BitmapFactory.decodeResource(resources, R.drawable.defaultavatar)) }
}
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?) { context?.let { imageView.addImage(resource) } }
})
fun Context.bitMapFromImgUrl(imageUrl: String, callBack: (bitMap: Bitmap) -> Unit) {
GlideApp.with(this)
.asBitmap()
.load(imageUrl)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
callBack(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
}
val bitmap = Glide.with(context)
.asBitmap()
.load("url")
.submit()
.get()