Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android下载的一系列文件不工作_Java_Android_Android Layout_Download_Android Asynctask - Fatal编程技术网

Java Android下载的一系列文件不工作

Java Android下载的一系列文件不工作,java,android,android-layout,download,android-asynctask,Java,Android,Android Layout,Download,Android Asynctask,我需要通过传递名称和URL下载文件的功能,这样我就可以在android应用程序中下载一系列文件 所以我们继续创造了这样的东西: public void doDownload (String fileName, String url){ File root = android.os.Environment.getExternalStorageDirectory(); File dir = new File (root.getAbsolutePath()

我需要通过传递名称和URL下载文件的功能,这样我就可以在android应用程序中下载一系列文件

所以我们继续创造了这样的东西:

public void doDownload (String fileName, String url){
    File root = android.os.Environment.getExternalStorageDirectory();               
    File dir = new File (root.getAbsolutePath() + "/Content/"); 
    if(dir.exists()==false) {
        dir.mkdirs();
    }

    try{
        URL url = new URL(url);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();
        Log.i("ANDRO_ASYNC", "Lenght of file: " + fileLength);

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(dir + fileName);   
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;

            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    }catch(Exception e){
    }
} 
XML:添加了权限

现在,当我调用此函数时:

doDownload(String img.png, String http://server.com);
它不返回任何文件。
这里出了什么问题?

另一个选项是使用下载管理器下载文件。您可以尝试使用DownloadManager。请访问android开发者网站。检查此链接:

目前,你正在调用循环内的
doDownload
方法来下载多个文件?否,在用户选择选项后,我正在调用此方法,例如:一旦用户选择选项1,选项1文件url和名称将使用doDownload(url,文件名)执行,以下载文件并将其存储在SD卡中。请看类似的问题是,我确实尝试过像
protectedstring doInBackground(String…sUrl,String fileName)
这样的函数,并尝试传递文件名,但没有成功。您收到了什么错误消息?当你一步一步地检查你的代码时,它在哪里被破坏?