[英]My app is displaying the image url. How do i download the image so that it can display on my app
//my adapter. //我的适配器。 I'm trying to find a way to implement Picasso in the code below or the activity for it to display the image. 我正在尝试找到一种在下面的代码或显示图片的活动中实施毕加索的方法。 I'm also using a gridview for displaying the images. 我还使用gridview来显示图像。 The getImage() below is the method responsible for getting the url 下面的getImage()是负责获取url的方法
public CategoryViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mContext = itemView.getContext();
}
public void bindCategory(Category category) {
mNameTextView.setText(category.getImage());
}
}
}
You can get the InputStream
from the url, and decode a bitmap to display it in a ImageView
. 您可以从网址获取InputStream
,并解码位图以在ImageView
显示它。
try {
URL url = new URL(getImage());
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
I recommend you to use Picasso library, using which you can easily load images using following code: 我建议您使用Picasso库,通过该库,您可以使用以下代码轻松加载图像:
Picasso.with(this/*context*/).load(getImage()).into(imageView);
Edit: 编辑:
As you wished to get image directly from the method, here it goes: 正如您希望直接从方法中获取图像一样,它如下:
public Bitmap getImage(){
String imageUrl = "https://farm"+getFarm()+".staticflickr.com/"+getServer()
+"/"+getId()+"_"+getSecret()+"_m.jpg";
try {
URL url = new URL(imageUrl);
return BitmapFactory.decodeStream(url.openStream());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
You can open the Image in a Webview. 您可以在Web视图中打开图像。 Make sure you have a WebView component in your Activity Xml also Declare the WebView variable outside of your Class ( Global variable ) So that u can use it inside a function. 确保在Activity Xml中有一个WebView组件,并且还在类(全局变量)之外声明WebView变量,以便您可以在函数内使用它。
public void getImage(){
String imageUrl = "https://farm"+getFarm()+".staticflickr.com/"+getServer()+"/"+getId()+"_"+getSecret()+"_m.jpg";
mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + imageUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);
}
You can view the image like that , using a custom lib 您可以使用自定义库查看类似的图像
Picasso.with(this).load(getImage()).into(imageView);
or 要么
Glide.with(this).load(getImage()).into(imageView);
and downloading the image: 并下载图片:
public void DownloadImageFromPath(String path){
InputStream in =null;
Bitmap bmp=null;
ImageView iv = (ImageView)findViewById(R.id.img1);
int responseCode = -1;
try{
URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.connect();
responseCode = con.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
//download
in = con.getInputStream();
bmp = BitmapFactory.decodeStream(in);
in.close();
iv.setImageBitmap(bmp);
}
}
catch(Exception ex){
Log.e("Exception",ex.toString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.