Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
从android中的php服务器下载pdf_Android_File_Download - Fatal编程技术网

从android中的php服务器下载pdf

从android中的php服务器下载pdf,android,file,download,Android,File,Download,我创建了一个php代码,用于从服务器下载pdf,效果很好。现在我想在Android id应用程序中使用这个php文件,并从Android服务器下载pdf。我该怎么做?我的php代码: <?php $id = $_GET['bookid']; $sql = "SELECT file,BookName FROM books WHERE id=" . prepareSQL($id) . " "; $query = mysql_query($sql) or die('Error, quer

我创建了一个php代码,用于从服务器下载pdf,效果很好。现在我想在Android id应用程序中使用这个php文件,并从Android服务器下载pdf。我该怎么做?我的php代码:

<?php
$id    = $_GET['bookid'];
$sql = "SELECT file,BookName FROM books WHERE id=" . prepareSQL($id) . " ";

$query = mysql_query($sql) or die('Error, query failed');
$content =  mysql_fetch_array($query);

header("Content-length: $size");
header("Content-type: $type");
$name=$content['BookName'];
header("Content-Disposition: attachment; filename=$name");

echo $content['file'];
?>

您可以使用这个

public static void downloadAndOpenPDF(final Context context, final String pdfUrl) {
    // Get filename
    final String filename = pdfUrl.substring( pdfUrl.lastIndexOf( "/" ) + 1 );
    // The place where the downloaded PDF file will be put
    final File tempFile = new File( context.getExternalFilesDir( Environment.DIRECTORY_DOWNLOADS ), filename );
    if ( tempFile.exists() ) {
        // If we have downloaded the file before, just go ahead and show it.
        openPDF( context, Uri.fromFile( tempFile ) );
        return;
    }

    // Show progress dialog while downloading
    cm=new CustomLoaderDialog(context);
    cm.show(true);
    //final ProgressDialog progress = ProgressDialog.show( context, "AIH" , "Please Wait", true );

    // Create the download request
    DownloadManager.Request r = new DownloadManager.Request(Uri.parse(pdfUrl));
    r.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,filename);
    final DownloadManager dm = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE );
    BroadcastReceiver onComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ( !cm.isShowing() ) {
                return;
            }
            context.unregisterReceiver( this );

            cm.hide();
            long downloadId = intent.getLongExtra( DownloadManager.EXTRA_DOWNLOAD_ID, -1 );
            Cursor c = dm.query( new DownloadManager.Query().setFilterById( downloadId ) );

            if ( c.moveToFirst() ) {
                int status = c.getInt( c.getColumnIndex( DownloadManager.COLUMN_STATUS ) );
                if ( status == DownloadManager.STATUS_SUCCESSFUL ) {
                    openPDF( context, Uri.fromFile( tempFile ) );
                }
            }
            c.close();
        }
    };
    context.registerReceiver( onComplete, new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE ) );

    // Enqueue the request
    dm.enqueue( r );
}