Java 如何将url图像下载到现有列表视图
我有一个自定义的listview,它有自己的基本适配器。目前它有一个标题和描述。我想从url添加一个图像。但我不知道如何调整代码以添加所述图像 当listview中的项目被按下时,我已经在使用异步任务下载文件了。我可以重用此方法来添加图像吗 我的文件下载异步任务仅在此时执行Java 如何将url图像下载到现有列表视图,java,android,url,imageview,Java,Android,Url,Imageview,我有一个自定义的listview,它有自己的基本适配器。目前它有一个标题和描述。我想从url添加一个图像。但我不知道如何调整代码以添加所述图像 当listview中的项目被按下时,我已经在使用异步任务下载文件了。我可以重用此方法来添加图像吗 我的文件下载异步任务仅在此时执行 class DownloadFileFromURL extends AsyncTask<String, String, String> { /** * Before starting back
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/Download/"+newnamestring);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
如果您想从URL下载图像并将其显示到您的ImageView中,您可以做的最好的事情是使用库,此库可以将图像下载并显示到您的ImageView中,并将其缓存以供下次使用, 这就是在添加到项目后如何使用它;
Glide.with(mContext).load(imageUrl).into(yourImageView)代码>#哈米德:谢谢你,我想我可能已经把glide图书馆忘得一干二净了
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
dataListAdapter() {
Title = null;
Detail = null;
;
}
public dataListAdapter(String[] text, String[] text1 ) {
Title = text;
Detail = text1;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
title = (TextView) row.findViewById(R.id.titleapp);
detail = (TextView) row.findViewById(R.id.detail);
title.setText(Title[position]);
detail.setText(Detail[position]);
return (row);
}
}