繁体   English   中英

Android调用php脚本下载文件并将其保存在SD卡中

[英]Android call php script to download file and save it in sd-card

我是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下载图像。 您需要创建一个类,该类将从网站流式传输文件并将其保存到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();
    }

希望这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM