即使提供了不同的 URL,也会下载具有相同名称的相同文件

Same file with same name getting downloaded even when provided with different URLs

我有一个 activity,它包含一个列表视图并在通过 onitemclicklistener 中的意图点击不同的列表项时发送不同的 url 在单击列表项时打开的 singleitemview activity 中,我有一个下载图像的按钮

问题是即使通过不同的列表项点击提供了不同的 URL,也只有 1 张图片被下载

我认为这是因为只提供了 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

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();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream
            OutputStream output = new FileOutputStream("/sdcard/download/loadedfile.png");

            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) {
            //e.printStackTrace();

            Log.e("Error: ","Error Message ::" +  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 downurl) {
        // dismiss the dialog after the file was downloaded


        dismissDialog(progress_bar_type);

        Toast.makeText(SingleItemView.this,
                       getString(R.string.download_complete),
                       Toast.LENGTH_SHORT).show();


    }

}

1

我在 singlitemview 中的下载按钮的 onclick 方法 activity

Intent i = getIntent();
file_url = i.getStringExtra("download");
public void downloadnow(){

  // different url is recived from different list item click via intent
    String downurl = file_url;

    new DownloadFileFromURL().execute(downurl);
}

尝试将文件名 loadedfile.png 更改为下载的文件名。

类似于:

String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
OutputStream output = new FileOutputStream("/sdcard/download/" + filename);