相关疑难解决方法(0)

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

我的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 …
Run Code Online (Sandbox Code Playgroud)

android android-input-method android-download-manager

24
推荐指数
1
解决办法
5万
查看次数