从Internet下载文件到我的下载文件夹

时间:2016-09-06 16:53:01

标签: android download storage internal

我在互联网上搜索了一个应用程序,我写了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();
    }
}

1 个答案:

答案 0 :(得分:0)

几年后,

add_action( 'plugins_loaded', 'includes' ); function includes() { remove_action( 'woocommerce_loaded', array( $GLOBALS['wc_bookings'], 'includes' ) ); } 未在Android上指出removable storage永远不要硬编码路径

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

btn-outline-primary

使用:

/sdcard