如何从服务器下载文件并将其保存在Android中SD卡的特定文件夹中? [英] How to download a file from a server and save it in specific folder in SD card in Android?
问题描述
我的Android应用程序中有一个要求.我需要以编程方式下载文件并将其保存在SD卡的特定文件夹中.我已经开发了源代码
I have one requirement in my Android application. I need to download and save file in specific folder of SD card programmatically. I have developed source code, which is
String DownloadUrl = "http://myexample.com/android/";
String fileName = "myclock_db.db";
DownloadDatabase(DownloadUrl,fileName);
// and the method is
public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/myclock/databases");
if(dir.exists() == false){
dir.mkdirs();
}
URL url = new URL("http://myexample.com/android/");
File file = new File(dir,fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager" , "download url:" +url);
Log.d("DownloadManager" , "download file name:" + fileName);
URLConnection uconn = url.openConnection();
uconn.setReadTimeout(TIMEOUT_CONNECTION);
uconn.setConnectTimeout(TIMEOUT_SOCKET);
InputStream is = uconn.getInputStream();
BufferedInputStream bufferinstream = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while((current = bufferinstream.read()) != -1){
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream( file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");
int dotindex = fileName.lastIndexOf('.');
if(dotindex>=0){
fileName = fileName.substring(0,dotindex);
}
catch(IOException e) {
Log.d("DownloadManager" , "Error:" + e);
}
}
现在问题是路径中仅保存了文件名为myclock_db.db的空文件.但是我需要下载文件的内容并将其保存在特定的文件夹中.尝试了几种下载文件的方法,但不能.
Now the issue is only empty file with filename myclock_db.db is saving in the path. but I need to download and save content of file in the specific folder. Tried several ways to get the file download, but I can't.
推荐答案
您的下载URL不是指向任何文件的链接.这是一个目录.确保其文件存在.还要检查您的logcat窗口以获取错误日志.还有一个建议,最好在catch块而不是Logs中执行printStackTrace().它给出了该错误的更详细视图.
Your download URL is not a link to any file. It's a directory. Make sure its a file and exists. Also check your logcat window for error logs. One more suggestion, its always better to do a printStackTrace() in catch blocks instead of Logs. Its gives a more detailed view of the error.
更改此行:
URL url = new URL("http://myexample.com/android/");
收件人:
URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url
接下来,在catch块中,添加以下行:
Next, in catch block, add this line:
e.printStackTrace();
也在目录路径中,应该是这样的:
Also in the directory path, it should be something like this:
File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");
代替
File dir = new File(root.getAbsolutePath() + "/myclock/databases");
下一步,确保您已获得在Android清单中写入外部存储的权限.
Next, make sure you have acquired permission for writing to external storage in Android manifest.
这篇关于如何从服务器下载文件并将其保存在Android中SD卡的特定文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!