如何在SD卡下载文件和存储?

时间:2014-06-26 03:47:27

标签: android performance android-activity

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

但是我得到了例外:java.io.filenotfoundexception (permission denied)

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;
        File file,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 file
                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 fileLength = connection.getContentLength();

                // download the file
                input = connection.getInputStream(); '



    GEtting exception in line '
                   output = new FileOutputStream("sdcard/file.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 (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;
        }

3 个答案:

答案 0 :(得分:1)

这样做,

        File root = new File(Environment.getExternalStorageDirectory(), "file.mp3");
            if (!root.exists()) {
                root.mkdirs();
            }

     output = new FileOutputStream(root);

并在Android menfestfile中添加此权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:1)

试试这段代码:

File yourFile = new File(Environment.getExternalStorageDirectory(),"yourfile.txt");   
// replace yourfile.txt with "file.mp3" your file name
if(!yourFile.exists()) {
    yourFile.createNewFile();
} 
FileOutputStream output = new FileOutputStream(yourFile, false); 

另外,请不要忘记在清单文件中添加此权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你还需要检查你的SD卡的安装天气与否,

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

  // Mounted
}
else {

}

答案 2 :(得分:1)

试试这个

File root = new File(Environment.getExternalStorageDirectory(), "file.mp3");
        if (!root.exists()) {
            root.mkdirs();
        }
output = new FileOutputStream(root);

并将清单文件的权限添加为

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />