Android 从url下载文件并保存到内存磁盘

Android 从url下载文件并保存到内存磁盘,android,download,Android,Download,我试图从url下载一个文件并将其保存到内存车,但我无法理解我的错误,我的代码是错误的 URL url = new URL(imageURL); File file = new File(fileName); long startTime = System.currentTimeMillis(); Log.d("ImageManager", "download begining"); Log.d("ImageManager", "d

我试图从url下载一个文件并将其保存到内存车,但我无法理解我的错误,我的代码是错误的

URL url = new URL(imageURL);
        File file = new File(fileName);

        long startTime = System.currentTimeMillis();
        Log.d("ImageManager", "download begining");
        Log.d("ImageManager", "download url:" + url);
        Log.d("ImageManager", "downloaded file name:" + fileName);
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
           baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
        Log.d("ImageManager", "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
但是我在控制台中发现了以下错误

12-27 14:50:01.302: D/EXCEPTION GET:(8062): java.io.FileNotFoundException: /my.jpg (Read-only file system)

实际上我不知道我能做什么,请任何人帮助。

问题在于您保存文件的路径,文件名不应以
/
开头,并将此修改应用于您的代码

File extStore = Environment.getExternalStorageDirectory();
File file = new File(extStore, fileName);

在尝试保存之前,您应该先保存。

请查看此
http://stackoverflow.com/questions/8687849/how-can-i-download-a-file-to-my-downloads-folder-android