即使提供了不同的网址

时间:2016-02-27 13:31:20

标签: android download outputstream

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

问题是,即使通过不同的列表项目点击提供不同的网址,也只会下载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>
{



    /**
     * 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方法

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 个答案:

答案 0 :(得分:1)

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

类似的东西:

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