Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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应用程序打开PDF(在单独的PDF查看器应用程序中)_Android_Pdf - Fatal编程技术网

如何从Android应用程序打开PDF(在单独的PDF查看器应用程序中)

如何从Android应用程序打开PDF(在单独的PDF查看器应用程序中),android,pdf,Android,Pdf,目标是能够将PDF文件添加到应用程序的资产文件夹中,并允许用户在PDF应用程序ezPDFReader、Adobe Reader等中打开该PDF。这并不完全简单,因为PDF首先需要从资产文件夹复制到设备存储中。如果设备上没有PDF viewer应用程序,我们需要显示一条警告消息,并将其引导到Play Store下载。以下是对我有效的解决方案。这是一个名为PdfHandler的类,下面是如何使用它的-只需将pdf文件放在assets文件夹中。它将在PDF查看器应用程序中打开它(如果有),如果没有,则

目标是能够将PDF文件添加到应用程序的资产文件夹中,并允许用户在PDF应用程序ezPDFReader、Adobe Reader等中打开该PDF。这并不完全简单,因为PDF首先需要从资产文件夹复制到设备存储中。如果设备上没有PDF viewer应用程序,我们需要显示一条警告消息,并将其引导到Play Store下载。

以下是对我有效的解决方案。这是一个名为PdfHandler的类,下面是如何使用它的-只需将pdf文件放在assets文件夹中。它将在PDF查看器应用程序中打开它(如果有),如果没有,则显示警告消息并将用户指向Adobe Reader Play商店条目

PdfHandler pdf = new PdfHandler(this);
pdf.openPdf("document.pdf");
PdfHandler类。您应该将文本资源移动到Android字符串资源中

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Handles the opening of PDF files, using whatever app the user has installed to view PDFs
 * If no PDF app installed, shows a warning and directs them to appropriate Store listing
 */
public class PdfHandler {

    public PdfHandler(Context context) {
        this.context = context;
    }

    public void openPdf(String filename) {
        if (isPdfAppAvailable()) {
            copyPdfAndOpenIt(filename);
        } else {
            showPdfWarning();
        }
    }

    private void copyPdfAndOpenIt(String filename) {
        try {
            File file = copyPdfFromAssetsToStorage(filename);
            startPdfIntent(file);
        } catch (Exception e) {
            Log.e("PdfHandler", "Error handling the PDF file", e);
        }
    }

    private File copyPdfFromAssetsToStorage(String filename) throws Exception {
        String tempFilename = "temp.pdf";
        AssetManager is = context.getAssets();
        InputStream inputStream = is.open(filename);
        String outFilename = context.getFilesDir() + "/" + tempFilename;
        FileOutputStream outputStream = context.openFileOutput(tempFilename, Context.MODE_WORLD_READABLE);
        copy(inputStream, outputStream);
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        return new File(outFilename);
    }

    private void copy(InputStream fis, FileOutputStream fos) throws IOException {
        byte[] b = new byte[8];
        int i;
        while ((i = fis.read(b)) != -1) {
            fos.write(b, 0, i);
        }
    }

    private boolean isPdfAppAvailable() {
        PackageManager packageManager = context.getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    private void startPdfIntent(File file) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        context.startActivity(intent);
    }

    private void showPdfWarning() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Please install an app to view PDF file");
        builder.setCancelable(false);
        builder.setPositiveButton("Install", getButtonListener());
        builder.setNegativeButton("Cancel", null);
        builder.setTitle("PDF Viewer");
        AlertDialog alert = builder.create();
        alert.show();
    }

    private DialogInterface.OnClickListener getButtonListener() {
        return new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    goToGooglePlayStoreEntry();
                }
            };
    }

    private void goToGooglePlayStoreEntry() {
        context.startActivity(getAppListingIntent());
    }

    private Intent getAppListingIntent() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String pdfAppPackageName = "com.adobe.reader";
        intent.setData(Uri.parse("market://details?id=" + pdfAppPackageName));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    private Context context;

}

这是对我有效的解决方案。这是一个名为PdfHandler的类,下面是如何使用它的-只需将pdf文件放在assets文件夹中。它将在PDF查看器应用程序中打开它(如果有),如果没有,则显示警告消息并将用户指向Adobe Reader Play商店条目

PdfHandler pdf = new PdfHandler(this);
pdf.openPdf("document.pdf");
PdfHandler类。您应该将文本资源移动到Android字符串资源中

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Handles the opening of PDF files, using whatever app the user has installed to view PDFs
 * If no PDF app installed, shows a warning and directs them to appropriate Store listing
 */
public class PdfHandler {

    public PdfHandler(Context context) {
        this.context = context;
    }

    public void openPdf(String filename) {
        if (isPdfAppAvailable()) {
            copyPdfAndOpenIt(filename);
        } else {
            showPdfWarning();
        }
    }

    private void copyPdfAndOpenIt(String filename) {
        try {
            File file = copyPdfFromAssetsToStorage(filename);
            startPdfIntent(file);
        } catch (Exception e) {
            Log.e("PdfHandler", "Error handling the PDF file", e);
        }
    }

    private File copyPdfFromAssetsToStorage(String filename) throws Exception {
        String tempFilename = "temp.pdf";
        AssetManager is = context.getAssets();
        InputStream inputStream = is.open(filename);
        String outFilename = context.getFilesDir() + "/" + tempFilename;
        FileOutputStream outputStream = context.openFileOutput(tempFilename, Context.MODE_WORLD_READABLE);
        copy(inputStream, outputStream);
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        return new File(outFilename);
    }

    private void copy(InputStream fis, FileOutputStream fos) throws IOException {
        byte[] b = new byte[8];
        int i;
        while ((i = fis.read(b)) != -1) {
            fos.write(b, 0, i);
        }
    }

    private boolean isPdfAppAvailable() {
        PackageManager packageManager = context.getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    private void startPdfIntent(File file) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        context.startActivity(intent);
    }

    private void showPdfWarning() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Please install an app to view PDF file");
        builder.setCancelable(false);
        builder.setPositiveButton("Install", getButtonListener());
        builder.setNegativeButton("Cancel", null);
        builder.setTitle("PDF Viewer");
        AlertDialog alert = builder.create();
        alert.show();
    }

    private DialogInterface.OnClickListener getButtonListener() {
        return new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    goToGooglePlayStoreEntry();
                }
            };
    }

    private void goToGooglePlayStoreEntry() {
        context.startActivity(getAppListingIntent());
    }

    private Intent getAppListingIntent() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String pdfAppPackageName = "com.adobe.reader";
        intent.setData(Uri.parse("market://details?id=" + pdfAppPackageName));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    private Context context;

}

Ollie C发布的PdfHandler工作得很好,但有点慢,因为它在打开PDF之前先将PDF从资产复制到存储。如果改用ContentProvider,打开PDF的速度会更快

请注意,如果文件名为MyPdf.pdf,则该文件应为assets/public_pdfs/MyPdf.pdf.mp3。public_pdf的路径是只导出您真正想要导出的pdf,而.mp3扩展名是为了防止压缩

AndroidManifest.xml

PdfContentProvider.java


Ollie C发布的PdfHandler工作得很好,但有点慢,因为它在打开PDF之前先将PDF从资产复制到存储。如果改用ContentProvider,打开PDF的速度会更快

请注意,如果文件名为MyPdf.pdf,则该文件应为assets/public_pdfs/MyPdf.pdf.mp3。public_pdf的路径是只导出您真正想要导出的pdf,而.mp3扩展名是为了防止压缩

AndroidManifest.xml

PdfContentProvider.java


+1.非常有用。一个很好的增强是可以选择缓存文件,这样在您希望更快地加载和更少的电池消耗,并且不太担心持久性存储使用的情况下,就不必每次都复制文件。我感谢您的努力。在应用程序之外打开pdf对其他人来说真的很有帮助。你能建议我如何在没有行动意图的情况下在我的应用程序中打开pdf。非常有用。一个很好的增强是可以选择缓存文件,这样在您希望更快地加载和更少的电池消耗,并且不太担心持久性存储使用的情况下,就不必每次都复制文件。我感谢您的努力。在应用程序之外打开pdf对其他人来说真的很有帮助。你能建议我如何在没有行动意图的情况下在我的应用程序中打开pdf吗。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

Uri uri = Uri.parse("content://my.app.PdfContentProvider/" + filename);
intent.setDataAndType(uri, "application/pdf");

startActivity(intent);
public class PdfContentProvider extends ContentProvider
{
  private static final String PDFPATH = "public_pdfs/";

  @Override
  public String getType(Uri uri)
  {
    return "application/pdf";
  }

  @Override
  public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException
  {
    AssetManager am = getContext().getAssets();
    String file_name = uri.getLastPathSegment();

    if (file_name == null) throw new FileNotFoundException();
    AssetFileDescriptor afd = null;
    try
    {
      afd = am.openFd(PDFPATH + file_name + ".mp3");
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return afd;
  }

  private final static String[] COLUMNS = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};

  @Override
  /**
   * This function is required for it to work on the Quickoffice at Android 4.4 (KitKat)
   */
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
  {
    if (projection == null)
    {
      projection = COLUMNS;
    }

    String[] cols = new String[projection.length];
    Object[] values = new Object[projection.length];
    int i = 0;
    for (String col : projection)
    {
      if (OpenableColumns.DISPLAY_NAME.equals(col))
      {
        cols[i] = OpenableColumns.DISPLAY_NAME;
        values[i++] = uri.getLastPathSegment();
      }
      else if (OpenableColumns.SIZE.equals(col))
      {
        cols[i] = OpenableColumns.SIZE;
        values[i++] = AssetFileDescriptor.UNKNOWN_LENGTH;
      }
    }

    cols = copyOf(cols, i);
    values = copyOf(values, i);

    final MatrixCursor cursor = new MatrixCursor(cols, 1);
    cursor.addRow(values);
    return cursor;
  }

  private static String[] copyOf(String[] original, int newLength)
  {
    final String[] result = new String[newLength];
    System.arraycopy(original, 0, result, 0, newLength);
    return result;
  }

  private static Object[] copyOf(Object[] original, int newLength)
  {
    final Object[] result = new Object[newLength];
    System.arraycopy(original, 0, result, 0, newLength);
    return result;
  }

  @Override
  public boolean onCreate()
  {
    return true;
  }

  @Override
  public Uri insert(Uri uri, ContentValues values)
  {
    return null;
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs)
  {
    return 0;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
  {
    return 0;
  }
}