如何将url图像下载到现有的列表视图
问题描述:
我有自己的基本适配器的自定义列表视图。它目前有一个标题和描述。编号喜欢从网址添加图片。但我不知道如何调整代码添加添加所述图像。如何将url图像下载到现有的列表视图
我已经在使用异步任务下载文件,当列表视图中的项目被按下时,我可以重复使用这种方法来添加图像吗?文件只在瞬间下载
我的异步任务
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);
}
}
我的自定义基本适配器
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);
}
}
答
,如果你想从URL下载图像,并显示
是到您的ImageView您可以将最好的东西做的就是使用Glide库,这个库可以下载Image并显示到你的imageView中,并将它缓存到另一个时间使用,这就是你在添加到你的项目后如何使用它的方法; Glide.with(mContext).load(imageUrl).into(yourImageView);
#哈米德谢谢我认为我可能有滑翔库忘记了所有关于那个欢呼 –
欢迎您;) –