下载从URL文件并保存到内存盘的Android

问题描述:

我试图从URL中下载文件并将其保存到内存车,但我不能明白什么是我的错,我的代码是下载从URL文件并保存到内存盘的Android

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) 

其实我不知道我能做什么,请任何人帮忙。

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

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

您应该在尝试保存之前check media availability

+0

请看这个'http:// *.com/questions/8687849 /如何下载文件到我的下载文件夹-Android' – Pritom 2011-12-31 11:34:55