【问题标题】:download file from internet to my download folder从互联网下载文件到我的下载文件夹
【发布时间】:2016-09-06 16:53:01
【问题描述】:

我在互联网上搜索了一个我编写 url 并按下按钮下载该文件但我没有 SD 卡的应用程序我想编辑此代码以使其下载到我设备的下载文件夹中而不是 SD 卡我应该做的

ProgressDialog mProgressDialog;
public void buRun(View view) {

    mProgressDialog = new ProgressDialog(MainActivity .this);
    mProgressDialog.setMessage("file is start downlaoding");
    mProgressDialog.setTitle("File donlaod ");
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setCancelable(true);


    final DownloadTask downloadTask = new DownloadTask(MainActivity.this);
    EditText txturl=(EditText)findViewById(R.id.txturl);
    downloadTask.execute(txturl.getText().toString());

    mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener()   {
        @Override
        public void onCancel(DialogInterface dialog) {
            downloadTask.cancel(true);
        }
    });


}

private class DownloadTask extends AsyncTask<String, Integer, String> {

    private Context context;
    private PowerManager.WakeLock mWakeLock;

    public DownloadTask(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            int fileLength = connection.getContentLength();

            String[] filen=sUrl[0].split("/");
            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream("/sdcard/Downlaod_" + filen[filen.length-1]);

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {

                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;

                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
        mWakeLock.acquire();

        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // if we get here, length is known, now set indeterminate to false
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        mProgressDialog.dismiss();
        if (result != null)
            Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

    标签: android download storage internal


    【解决方案1】:

    /sdcard 已经好几年没有在 Android 上指向 removable storage 了。 从不硬编码路径

    要下载到external storage 上的标准下载位置,请替换:

    output = new FileOutputStream("/sdcard/Downlaod_" + filen[filen.length-1]);
    

    与:

    output =
      new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filen[filen.length-1]));
    

    【讨论】:

    • 我认为他想将其保存到内部存储中,因为 OP 说“我没有 SD 卡”
    • @BAAAZINGA:SD 卡在大多数设备上不是 external storage。它是removable storage。问题指出“我想编辑此代码以使其下载到我设备的下载文件夹中”。 internal storage 没有“我的设备中的下载文件夹”,至少不是 OP 可以看到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2011-10-14
    • 2011-11-28
    • 2013-11-30
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多