【问题标题】:Android call php script to download file and save it in sd-cardAndroid调用php脚本下载文件并保存在sd-card中
【发布时间】:2012-07-08 03:00:38
【问题描述】:

我是 php 的新手。我尝试在 android 中制作一个从 eClass 平台下载文件的应用程序。我开发了这个脚本:

    <?php
            $code=$_GET['code'];
            $code = stripslashes($code);
            $code = mysql_real_escape_string($code);

            $filename=$_GET['filename'];
            $filename = stripslashes($filename);
            $filename = mysql_real_escape_string($filename);

            $path = "C:\\xampp\\htdocs\\openeclass-2.5\\courses\\".$code."\\dropbox\\".$filename;
            $fullPath = $path.$_GET['download_file'];

            if ($fd = fopen ($fullPath, "r")) {
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext = strtolower($path_parts["extension"]);
                switch ($ext) {
                    case "pdf":
                    header("Content-type: application/pdf"); // add here more headers for diff. extensions
                    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // 

            use 'attachment' to force a download
                    break;
                    default;
                    header("Content-type: application/octet-stream");
                    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                }
                header("Content-length: $fsize");
                header("Cache-control: private"); //use this to open files directly
                while(!feof($fd)) {
                    $buffer = fread($fd, 2048);
                    echo $buffer;
                }
            }
            fclose ($fd);
            exit;
            ?>

从浏览器(在我的本地主机中)调用时下载文件,并将代码和文件名作为 url 中的参数。现在我想在我的 android 应用程序中构建一个调用这个脚本的函数,获取文件并将其保存在 sd 卡中的某个位置

我怎样才能做到这一点? 也可以在模拟器中完成吗?提前谢谢!

【问题讨论】:

    标签: php android sd-card file-transfer


    【解决方案1】:

    很遗憾,您不能通过 php 强制下载图片。您需要创建一个类,该类将从网络位置流式传输文件并将其保存到 sd 卡。

    我的应用中有类似的代码可以同时执行这两种操作:

    try {
        newurl = new URL(iurl); // iurl is a String value
            b = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    try {
        b.compress(CompressFormat.JPEG, 100, new FileOutputStream(
            sdcard_image)); // sdcard_image is a String value of local filename (including dir)
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    

    希望这会有所帮助

    【讨论】:

    • 谢谢,你能解释一下 sdcard_image 应该如何声明吗?这不是应该像 File sdcard_image =Environment.getExternalStorageDirectory(); 这样的文件吗? ?我也不想只下载图片,也不想下载其他类型的文件(例如 txt)
    • here - 无论是什么类型的文件,都是写入/读取文件的示例。下载需要BufferedInputStreamFile
    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多