Java 如何将url图像加载到位图,将其转换为可绘制图像并使用imageview显示
我有一个Java 如何将url图像加载到位图,将其转换为可绘制图像并使用imageview显示,java,android,firebase,android-fragments,Java,Android,Firebase,Android Fragments,我有一个firebase的uri图像,但我不知道如何将其加载到位图中,因为我需要剪切图像的四角边缘 我用resourceid尝试了它,提供了可绘制的图标,效果不错,但在uri 下面是我的代码: uri imagefile = model.getImageUri(); if (imagefile !=null){ imageView.setVisibility(View.VISIBLE); Resources res = c.
firebase
的uri
图像,但我不知道如何将其加载到位图中,因为我需要剪切图像的四角边缘
我用resourceid
尝试了它,提供了可绘制的图标,效果不错,但在uri
下面是我的代码:
uri imagefile = model.getImageUri();
if (imagefile !=null){
imageView.setVisibility(View.VISIBLE);
Resources res = c.getResources();
//How i'm loading the image
Bitmap src = BitmapFactory.decodeResource(res,
Integer.parseInt(imagefile));
RoundedBitmapDrawable dr =
RoundedBitmapDrawableFactory.create(res, src);
dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) /
30.0f);
imageView.setImageDrawable(dr);
}
如何使用
uri
加载图像?它也将帮助我解决其他相关问题。谢谢您可以在这里查看如何将Uri转换为Url(查看Commonware的答案)
这就是如何从URL加载位图(看rajath的答案)
我建议您使用AsyncTask将流程保持在后台,以免延迟UI: ImageLoadAsyncTask.java
public class ImageLoadAsyncTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private ImageView imageView;
public ImageLoadAsyncTask(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
URL urlConnection = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
}
祝你好运 谢谢。我尝试了他的答案,但我得到了错误,因为代码正在主线程上运行。我低头寻找另一个答案,另一个答案改进了他自己的答案,但我不知道如何在我的代码中使用它。
ImageLoadAsyncTask imageLoadAsyncTask = new ImageLoadAsyncTask(url, imageView);
imageLoadAsyncTask.execute();