android-如何下载文件并存储在sdcard中?

android-如何下载文件并存储在sdcard中?,第1张

概述我正在研究它应该从服务器下载文件存储在sdcard中.但是我得到了异常:java.io.filenotfoundexception(权限被拒绝)publicclassMainActivityextendsActionBarActivity{//usually,subclassesofAsyncTaskaredeclaredinsidetheactivityclass.//thatway

我正在研究它应该从服务器下载文件并存储在sdcard中.

但是我得到了异常:java.io.@R_502_6852@notfoundexception(权限被拒绝)

public class MainActivity extends ActionBaractivity {    // usually, subclasses of AsyncTask are declared insIDe the activity class.    // that way, you can easily modify the UI thread from here    private class DownloadTask extends AsyncTask<String, Integer, String> {        private Context context;        private PowerManager.WakeLock mWakeLock;        @R_502_6852@ @R_502_6852@,sdcard;        public DownloadTask(Context context) {            this.context = context;        }        @OverrIDe        protected String doInBackground(String... sUrl) {            System.out.print("BAckground");            inputStream input = null;            OutputStream output = null;            httpURLConnection connection = null;            try {                URL url = new URL(sUrl[0]);                connection = (httpURLConnection) url.openConnection();                connection.connect();                // expect http 200 OK, so we don't mistakenly save error report                // instead of the @R_502_6852@                if (connection.getResponseCode() != httpURLConnection.http_OK) {                    return "Server returned http " + connection.getResponseCode()                            + " " + connection.getResponseMessage();                }                // this will be useful to display download percentage                // might be -1: server dID not report the length                int @R_502_6852@Length = connection.getContentLength();                // download the @R_502_6852@                input = connection.getinputStream(); '    GEtting exception in line '                   output = new @R_502_6852@OutputStream("sdcard/@R_502_6852@.mp3");                byte data[] = new byte[4096];                long total = 0;                int count;                while ((count = input.read(data)) != -1) {                    // allow canceling with back button                    if (isCancelled()) {                        input.close();                        return null;                    }                    total += count;                    // publishing the progress....                    if (@R_502_6852@Length > 0) // only if total length is kNown                        publishProgress((int) (total * 100 / @R_502_6852@Length));                    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;        }

解决方法:

尝试这个

@R_502_6852@ root = new @R_502_6852@(Environment.getExternalStorageDirectory(), "@R_502_6852@.mp3");        if (!root.exists()) {            root.mkdirs();        }output = new @R_502_6852@OutputStream(root);

并在清单文件上添加权限为

<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />
总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存