Android 从 URL 加载到位图
我有一个关于从网站加载图像的问题。我使用的代码是:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Bitmap bit=null;
try {
bit = BitmapFactory.decodeStream((InputStream)new URL("http://www.mac-wallpapers.com/bulkupload/wallpapers/Apple%20Wallpapers/apple-black-logo-wallpaper.jpg").getContent());
} catch (Exception e) {}
Bitmap sc = Bitmap.createScaledBitmap(bit,width,height,true);
canvas.drawBitmap(sc,0,0,null);
但是总是返回空指针异常,程序崩溃了。 该 URL 有效,并且似乎对其他人都有效。 我正在使用2.3.1。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果您使用 Picasso 或 Glide 或 Universal-Image-Loader 用于加载图像网址。
获取加载的位图
您可以简单地通过For Picasso (当前版本
2.71828
)Java 代码
Kotlin 代码
For Glide
检查 如何使用 glide 进行下载将图像转换为位图?
对于 Universal-Image-Loader
Java代码
If you are using Picasso or Glide or Universal-Image-Loader for load image from url.
You can simply get the loaded bitmap by
For Picasso (current version
2.71828
)Java code
Kotlin code
For Glide
Check How does one use glide to download an image into a bitmap?
For Universal-Image-Loader
Java code
我更喜欢这一个:
从InputStream创建位图并返回它:
记住:
I Prefer these one:
Creates Bitmap from InputStream and returns it:
REMEMBER :
此方法将使用 kotlin 协程来实现这一点,因此它不会阻塞 UI 主线程和线程。将返回调整大小的圆形位图图像(如个人资料图像)
This method will do the trick with kotlin coroutine so it won't block the UI Main thread & will return resized circle bitmap image(like profile image)
按照方法在 android 中获取位图的 url,只需传递该图像的链接并获取位图。
Follow method to get url to bitmap in android just pass link of this image and get bitmap.
它在 Pie OS 中工作使用这个
Its Working in Pie OS Use this
使用 Kotlin 协程处理线程
代码崩溃的原因是
Bitmap
试图在Main Thread
上创建,这是不允许的,因为它可能会导致 Android 无响应 (ANR) 错误。使用的概念
toBitmap()
是一个Kotlin 扩展函数 要求将该库添加到应用程序依赖项中。实现
代码
1. 在与
主线程
不同的线程中创建位图
。在此使用 Kotlin Coroutines 的示例中,函数在 Dispatchers.IO 线程中执行,该线程用于基于 CPU 的操作。该函数以
suspend
为前缀,这是一种 Coroutine 语法。奖励 - 创建
位图
后,它也会被压缩为ByteArray
,以便可以通过Intent
传递稍后在此完整示例中进行了概述。Repository.kt
ViewModel.kt
奖励 - 将
ByteArray
转换回Bitmap
。Utils.kt
Use Kotlin Coroutines to Handle Threading
The reason the code is crashing is because the
Bitmap
is attempting to be created on theMain Thread
which is not allowed since it may cause Android Not Responding (ANR) errors.Concepts Used
toBitmap()
is a Kotlin extension function requiring that library to be added to the app dependencies.Implementation
Code
1. Create
Bitmap
in a different thread then theMain Thread
.In this sample using Kotlin Coroutines the function is being executed in the
Dispatchers.IO
thread which is meant for CPU based operations. The function is prefixed withsuspend
which is a Coroutine syntax.Bonus - After the
Bitmap
is created it is also compressed into anByteArray
so it can be passed via anIntent
later outlined in this full sample.Repository.kt
ViewModel.kt
Bonus - Convert
ByteArray
back toBitmap
.Utils.kt
传递您的图片网址:
试试这个:
创建类实用程序:
Pass your Image URL:
Try this:
Create Class Utils:
如果您更喜欢 Coil 而不是 Glide。
If you prefer Coil instead of Glide.
试试这个:
Try this:
使用 Glide 库并在发布的工作线程中运行以下代码
Use Glide library and run the following code in work thread as posted
调用 asynctask 如下:
Call asynctask like:
非常快的方法,这个方法工作得非常快:
very fast way , this method works very quickly:
请尝试以下步骤。
1)在类或适配器中创建AsyncTask(如果要更改列表项图像)。
2) 从您的活动、片段或适配器(在 onBindViewHolder 内)调用 AsyncTask。
2.a) 对于适配器:
2.b) 对于活动和片段:
3) 请运行应用程序并检查图像。
快乐编码......:)
Please try this following steps.
1) Create AsyncTask in class or adapter(if you want to change the list item image).
2) Call the AsyncTask from your activity, fragment or adapter(inside onBindViewHolder).
2.a) For adapter:
2.b) For activity and fragment:
3) Kindly run the app and check the image.
Happy coding....:)
如果不使用 AsyncTask 从位图加载 URL,请在 setContentView(R.layout.abc); 后写入两行;
If you load URL from bitmap without using AsyncTask, write two lines after setContentView(R.layout.abc);
如果您使用Picasso for Images,您可以尝试以下方法!
If you are using Picasso for Images you can try method below!
如果您使用 Glide 和 Kotlin,
if you are using Glide and Kotlin,
和毕加索一起试试这个
Try this with picasso