如何在android中下载?

如何在android中下载?,第1张

概述我想使用Android代码(如下载管理器)从url创建下载应用程序但实际上我不知道如何开始感谢任何帮助或任何视频tuts解决方法:此代码将从url下载任何文件,只需替换url和位置..publicclassAndroidDownloadFileByProgressBarActivityextendsActivity{//buttontoshowpr

我想使用Android代码(如下载管理器)从url创建下载应用程序
但实际上我不知道如何开始

感谢任何帮助或任何视频tuts

解决方法:

此代码将从url下载任何文件,只需替换url和位置..

public class AndroIDDownloadfileByProgressBaractivity extends Activity {    // button to show progress dialog    button btnShowProgress    // Progress Dialog    private ProgressDialog pDialog;    // Progress dialog type (0 - for Horizontal progress bar)    public static final int progress_bar_type = 0;    // file url to download    private static String file_url = " u r l";    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        // show progress bar button        btnShowProgress = (button) findVIEwByID(R.ID.btnProgressbar);        // Image vIEw to show image after downloading        my_image = (ImageVIEw) findVIEwByID(R.ID.my_image);        /**         * Show Progress bar click event         * */        btnShowProgress.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                // starting new Async Task                new DownloadfileFromURL().execute(file_url);            }        });    }@OverrIDeprotected Dialog onCreateDialog(int ID) {    switch (ID) {    case progress_bar_type:        pDialog = new ProgressDialog(this);        pDialog.setMessage("Downloading file. Please wait...");        pDialog.setIndeterminate(false);        pDialog.setMax(100);        pDialog.setProgressstyle(ProgressDialog.STYLE_HORIZONTAL);        pDialog.setCancelable(true);        pDialog.show();        return pDialog;    default:        return null;    }}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();            // getting file length            int lenghtOffile = conection.getContentLength();            // input stream to read file - with 8k buffer            inputStream input = new BufferedinputStream(url.openStream(), 8192);            // Output stream to write file            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 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 file_url) {        // dismiss the dialog after the file was downloaded        dismissDialog(progress_bar_type);    }}

清单文件:

<!-- Permission: Allow Connect to Internet -->    <uses-permission androID:name="androID.permission.INTERNET" />    <!-- Permission: Writing to SDCard -->    <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />

main.xml中

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:orIEntation="vertical" >    <!-- Download button -->    <button androID:ID="@+ID/btnProgressbar"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:text="Download file with Progress bar"        androID:layout_margintop="50dip"/></linearLayout>
总结

以上是内存溢出为你收集整理的如何在android中下载?全部内容,希望文章能够帮你解决如何在android中下载?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存