无法在 Android 手机中下载文件 - Android..?

发布于 2025-01-04 06:33:53 字数 1917 浏览 0 评论 0原文

我在文件下载方面遇到问题,

我可以在模拟器中下载文件,但它不适用于手机。 我已经定义了互联网和写入SD卡的权限

我在服务器上有一个文档文件,如果用户单击下载。它下载该文件。这在模拟器中工作正常,但在手机中不起作用。

编辑 我的下载文件代码

public void downloadFile(String _url, String fileName) {
        File PATH = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        try {
            PATH.mkdirs();
            URL url = new URL(_url); // you can write here any link
            File file = new File(PATH, fileName);

            long startTime = System.currentTimeMillis();
            Log.d("Manager", "download begining");
            Log.d("DownloadManager", "download url:" + url);
            Log.d("DownloadManager", "downloaded file name:" + fileName);
            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */
            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");

        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }
    }

I having problem with file download,

I am able to download file in emulator but It is not working with the phone.
I have defined the permission for the Internet and write SD card.

I having one doc file on server, and if user click on download. It downloads the file. This works fine in emulator but not working in phone.

Edit
My code for download file

public void downloadFile(String _url, String fileName) {
        File PATH = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        try {
            PATH.mkdirs();
            URL url = new URL(_url); // you can write here any link
            File file = new File(PATH, fileName);

            long startTime = System.currentTimeMillis();
            Log.d("Manager", "download begining");
            Log.d("DownloadManager", "download url:" + url);
            Log.d("DownloadManager", "downloaded file name:" + fileName);
            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */
            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");

        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2025-01-11 06:33:53

尝试下面给出的片段...

File PATH = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            _url = _url.replace(" ", "%20");
            URL url = new URL(_url);

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);


            //and connect!
            urlConnection.connect();

            //create a new file, specifying the path, and the filename
            //which we want to save the file as.
            File file = new File(PATH,fileName);

            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            Log.i("Download", totalSize+"");
            //variable to store total downloaded bytes
//          int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                    //add the data in the buffer to the file in the file output stream (the file on the sd card
                    fileOutput.write(buffer, 0, bufferLength);
            }
            //close the output stream when done
            fileOutput.close();
            return true;

    //catch some possible errors...
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }

确保您输入了正确的下载路径(url)

try the snippets given bellow...

File PATH = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            _url = _url.replace(" ", "%20");
            URL url = new URL(_url);

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);


            //and connect!
            urlConnection.connect();

            //create a new file, specifying the path, and the filename
            //which we want to save the file as.
            File file = new File(PATH,fileName);

            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            Log.i("Download", totalSize+"");
            //variable to store total downloaded bytes
//          int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                    //add the data in the buffer to the file in the file output stream (the file on the sd card
                    fileOutput.write(buffer, 0, bufferLength);
            }
            //close the output stream when done
            fileOutput.close();
            return true;

    //catch some possible errors...
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }

make sure you have enters the correct download path(url)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文