【问题标题】:download pdf from php server in android从android中的php服务器下载pdf
【发布时间】:2015-11-03 11:34:19
【问题描述】:

我创建了一个用于从服务器下载 pdf 的 php 代码,它运行良好。现在我想在 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'];
?>

【问题讨论】:

    标签: android file download


    【解决方案1】:

    你可以用这个。

    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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多