简体   繁体   English

如何从片段类中的 URL 加载图像?

[英]How to load image from URL in Fragment class?

I want to load an image from its URL in Fragment class.我想从 Fragment 类中的 URL 加载图像。 I'm loading the image like this:我正在加载这样的图像:

try {
 // ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
  Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
  Bitmap conv_bm = getRoundedRectBitmap(resized, 255);
  i.setImageBitmap(conv_bm); 
 // j.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

but this method is taking more time to load the image and to open it.但是这种方法需要更多时间来加载图像并打开它。 If I want to use ImageLoader class like this tutorial , then how to load it in Fragment?如果我想像本教程一样使用ImageLoader类,那么如何在 Fragment 中加载它?

为什么不使用PicassoUniversal image loader

use any 3rd party libabry like picasso, 使用任何第三方的像libasso那样的毕加索,

https://github.com/square/picasso https://github.com/square/picasso

download the jar file and put the jar file inside your libs folder, 下载jar文件并将jar文件放入您的libs文件夹中,

for more info see, http://square.github.io/picasso/ 有关更多信息,请参见http://square.github.io/picasso/

in case of fragments, use getActivity() instead of context. 如果是碎片,请使用getActivity()代替上下文。

For rounded imageview,i use this https://github.com/vinc3m1/RoundedImageView 对于圆形的图像视图,我使用此https://github.com/vinc3m1/RoundedImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM