Same file with same name getting downloaded even w

2019-07-11 14:05发布

问题:

I have a activity that contains a listview and sends different url when different list item is clicked via intent in onitemclicklistener In the singleitemview activity which is opened when list item is clicked i have a button to download images

The problem is only 1 image is getting downloaded even different URLs are provided through different list item clicks

I think this is happening because of providing only 1 outputstream like this

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

how to change this so each different image gets downloaded with its own file name

onitemclicklistener of listview

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);
      }

My download 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

onclick method for my download button in singlitemview 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);
}

回答1:

Try to change file name loadedfile.png to downloaded file name.

Something like:

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