Java 我的应用程序正在显示图像url。如何下载图像以便在我的应用程序上显示
//我的适配器。 我正试图找到一种方法来实现毕加索在下面的代码或活动,它显示的图像。我还使用gridview来显示图像。下面的getImage()是负责获取url的方法Java 我的应用程序正在显示图像url。如何下载图像以便在我的应用程序上显示,java,android,api,flickr,Java,Android,Api,Flickr,//我的适配器。 我正试图找到一种方法来实现毕加索在下面的代码或活动,它显示的图像。我还使用gridview来显示图像。下面的getImage()是负责获取url的方法 public CategoryViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); mContext = itemView.getCont
public CategoryViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mContext = itemView.getContext();
}
public void bindCategory(Category category) {
mNameTextView.setText(category.getImage());
}
}
}
您可以从url获取
InputStream
,并解码位图以在ImageView
中显示它
try {
URL url = new URL(getImage());
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
我建议您使用毕加索库,使用该库,您可以使用以下代码轻松加载图像:
Picasso.with(this/*context*/).load(getImage()).into(imageView);
编辑:
由于您希望直接从该方法中获取图像,因此如下所示:
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;
}
}
您可以在网络视图中打开图像。确保活动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);
}
public void getImage(){
字符串imageUrl=”https://farm“+getFarm();
loadDataWithBaseURL(null,“,”html/css“,”utf-8“,null);
}
您可以使用自定义库查看这样的图像
Picasso.with(this).load(getImage()).into(imageView);
或
下载图片:
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());
}
}
是否要在调用getImage()时显示图像?是@LokkeshwaranJis有没有办法在调用getImage()时显示图像@grace检查编辑过的答案。在
AsyncTask
中使用此函数,因为此函数会阻止UI线程。我尝试使用此函数,但它不起作用。我有一个阵列适配器,无法将imageView设置为阵列适配器
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());
}
}