修改此代码(下载图像)以将图像上载到特定URL

时间:2016-03-21 15:16:19

标签: android android-studio android-volley progressdialog

我可以使用排球上传图片,但无法将其与进度对话框同步。我想要一个进度对话框与下载该文件所需的大小/时间同步。

我使用下面的代码下载图片:

       @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/downloadedfile.jpg");

            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 "completed";
    }

我想要更改上面的代码,以便它可以用于上传图像。

0 个答案:

没有答案