我有一个包含列表视图的活动,当通过onitemclicklistener中的intent点击不同的列表项时,它会发送不同的url
在单击列表项时打开的singleitemview活动中,我有一个下载图像的按钮
问题是,即使通过不同的列表项点击提供了不同的URL,也只有1张图片被下载
我认为这是因为只提供了一个这样的输出流
OutputStream output = new FileOutputStream("/sdcard/download/loadedfile.png");
如何更改此设置,以便每个不同的图像都以其自己的文件名下载
listview的onitemclicklistener
public void onItemClick(AdapterView<?> p1, View view, int position, long p4)
{
Intent intent = new Intent(ProActivity.this, SingleItemView.class);
intent.putExtra("download",
(codelist.get(position).getDownloadCode()));
staetActivity(intent);
}
我的下载类
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/download/loadedfile.png");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ","Error Message ::" + e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String downurl) {
dismissDialog(progress_bar_type);
Toast.makeText(SingleItemView.this,
getString(R.string.download_complete),
Toast.LENGTH_SHORT).show();
}
}
1.
singlitemview活动中下载按钮的onclick方法
Intent i = getIntent();
file_url = i.getStringExtra("download");
public void downloadnow(){
String downurl = file_url;
new DownloadFileFromURL().execute(downurl);
}