android - 如何从服务器下载文件并将其保存在 Android 中 SD 卡的特定文件夹中?

标签 android android-input-method android-download-manager

我的 Android 应用程序有一个要求。我需要以编程方式下载文件并将其保存在 SD 卡的特定文件夹中。我开发了源代码,就是

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 的空文件保存在路径中。但我需要下载文件内容并将其保存在特定文件夹中。尝试了几种方法来获取文件下载,但我不能。

最佳答案

您的下载 URL 不是任何文件的链接。这是一个目录。确保它是一个文件并且存在。还要检查您的 logcat 窗口中的错误日志。还有一个建议,在 catch block 而不是 Logs 中执行 printStackTrace() 总是更好。它提供了错误的更详细 View 。

改变这一行:

    URL url = new URL("http://myexample.com/android/");

到:

    URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url

接下来,在 catch block 中,添加这一行:

e.printStackTrace();

同样在目录路径中,应该是这样的:

File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");

而不是

File dir = new File(root.getAbsolutePath() + "/myclock/databases");

接下来,确保您已获得在 Android list 中写入外部存储的权限。

关于android - 如何从服务器下载文件并将其保存在 Android 中 SD 卡的特定文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16117067/

相关文章:

java - 如何检查 Android 设备是否支持库?

java - Android Update 3后Android资源链接失败

Android:弹出菜单位置

android - 从 InputConnection 获取 EditText 中所有文本的更好方法?

android - 下载管理器与 InputStream.read()

android - 由 : java. lang.IllegalStateException : Unable to create directory in Android 6. 0 设备引起

android - BottomNavigationView 点击后占用空间

android - 在 Activity 默认运行时显示数字键盘

Android 日志消息 "InputMethodManager: The current service view is not the focus view"

android - 分区存储 - 下载文件夹访问