【问题标题】:How to retrive pdf file from server & show in app & how?如何从服务器检索pdf文件并在应用程序中显示以及如何?
【发布时间】:2016-12-16 07:27:42
【问题描述】:

如何在 android 中将 pdf 文件转换为 Base64 以及如何使用 json(HTTPURLConection) 从服务器中检索 pdf 文件?

【问题讨论】:

  • 在此处发布问题之前,请先进行一些研究或练习基础知识。

标签: android json httpurlconnection pdf-viewer


【解决方案1】:

您可以使用

在 android 中将 PDF 转换为 base64
File dir = Environment.getExternalStorageDirectory();
    File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
    String encodeFileToBase64Binary = encodeFileToBase64Binary(yourFile);

    private static String encodeFileToBase64Binary(File fileName) throws IOException {
          byte[] bytes = loadFile(fileName);
          byte[] encoded = Base64.encodeBase64(bytes);
          String encodedString = new String(encoded);
          return encodedString;
     }

你可以参考这个链接来解决你的第二个问题Retrieve PDF from server

【讨论】:

    【解决方案2】:
    You can download a PDF fie from server using following Code..
    
    
    avtivity_main.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.aman.pdfserver.MainActivity">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="15dp"
            android:text="download"
            android:onClick="download" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button1"
            android:layout_marginTop="38dp"
            android:text="view"
            android:onClick="view" />
    
    </RelativeLayout>
    
    
    MainActivity.java
    
    
        package com.example.aman.pdfserver;
    
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import java.io.File;
        import java.io.IOException;
    
        import android.app.Activity;
        import android.app.ProgressDialog;
        import android.content.ActivityNotFoundException;
        import android.content.Intent;
        import android.net.Uri;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.os.Environment;
        import android.util.Log;
        import android.view.View;
        import android.widget.Toast;
    
        public class MainActivity extends AppCompatActivity {
    
            // Progress dialog
            private ProgressDialog pDialog;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                pDialog = new ProgressDialog(this);
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
            }
    
    
            public void download(View v)
            {
                new DownloadFile().execute("http://maven.apache.org/maven-1.x/maven.pdf", "maven.pdf");
            }
    
            public void view(View v)
            {
                File pdfFile = new File(Environment.getExternalStorageDirectory() + "/testthreepdf/" + "maven.pdf");  // -> filename = maven.pdf
                Uri path = Uri.fromFile(pdfFile);
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
                try{
                    startActivity(pdfIntent);
                }catch(ActivityNotFoundException e){
                    Toast.makeText(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
                }
            }
    
            private class DownloadFile extends AsyncTask<String, Void, Void>{
    
                @Override
                protected Void doInBackground(String... strings) {
                    String fileUrl = strings[0];   // -> http://maven.apache.org/maven-1.x/maven.pdf
                    String fileName = strings[1];  // -> maven.pdf
                    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                    File folder = new File(extStorageDirectory, "testthreepdf");
                    folder.mkdir();
    
                    File pdfFile = new File(folder, fileName);
    
                    try{
                        pdfFile.createNewFile();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                    FileDownloader.downloadFile(fileUrl, pdfFile);
                    return null;
                }
            }
    
    
        }
    
    
    FileDownloader.java
    
    
    package com.example.aman.pdfserver;
    
    /**
     * Created by Aman on 13-12-2016.
     */
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class FileDownloader {
        private static final int  MEGABYTE = 1024 * 1024;
    
        public static void downloadFile(String fileUrl, File directory){
            try {
    
                URL url = new URL(fileUrl);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
    
                urlConnection.connect();
    
                InputStream inputStream = urlConnection.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(directory);
                int totalSize = urlConnection.getContentLength();
    
                byte[] buffer = new byte[MEGABYTE];
                int bufferLength = 0;
                while((bufferLength = inputStream.read(buffer))>0 ){
                    fileOutputStream.write(buffer, 0, bufferLength);
                }
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-28
      • 2012-11-21
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多