2012-04-03 2 views
1

Целью является возможность добавления файла PDF в папку «assets» приложения и возможность пользователю открыть этот PDF-файл в Приложение PDF (ezPDFReader, Adobe Reader и т. Д.). Это не совсем просто, так как PDF сначала необходимо скопировать из папки с ресурсами на хранилище устройств. Если на устройстве нет приложения для просмотра PDF-файлов, нам нужно показать предупреждающее сообщение и направить их в Play Store для его загрузки.Как открыть PDF-файл из приложения для Android (в отдельном приложении для просмотра PDF-файлов)

ответ

6

Вот решение, которое сработало для меня. Это класс под названием PdfHandler, и вот как вы его используете - просто поместите файл pdf в папку с ресурсами. Он откроет его в приложении для просмотра PDF, если он доступен, и если не появится предупреждающее сообщение и направьте пользователя в запись в Adobe Reader Play Store.

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; 

} 
+0

+1. Очень полезно. Хорошим улучшением будет возможность кэшировать файлы, чтобы их не нужно было копировать каждый раз, в ситуациях, когда вы хотели бы быстрее загружать и меньше потреблять батареи, и не очень беспокоитесь о постоянном использовании хранилища. – Nate

+0

Я ценю ваши усилия. Это действительно помогает другим открыть PDF-приложение вне приложения. можете ли вы предложить мне, как я могу открыть PDF внутри своего приложения без намерений. –

0
LoginProgress task = new LoginProgress(); 
task.execute(Pdffile); 




public class LoginProgress extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog dialog; 
     protected Context applicationContext; 
     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      this.dialog.dismiss(); 
     } 
     @Override 
     protected void onPreExecute() { 
      this.dialog = ProgressDialog.show(applicationContext, 
        "Data Loading", "Loading .....", true); 
     } 
     @Override 
     protected Void doInBackground(Void... params) { 
      // video(); 
      LoadUrl(Pdffile); 
      return null; 
     } 
     private void LoadUrl(File Pdffile) { 
      try { 
       u r stuff here 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
1

PdfHandler отправленный Олли C работает отлично, но это немного медленно, потому что он копирует PDF первый из активов в хранилище, прежде чем он может быть открыт. Открытие PDF-файла будет быстрее, если вы используете ContentProvider.

Обратите внимание, что если ваше имя файла MyPdf.pdf, файл должен быть активом/public_pdfs/MyPdf.pdf.mp3. Путь public_pdfs заключается только в том, чтобы экспортировать pdf-файлы, которые вы действительно хотите экспортировать, а расширение .mp3 - для предотвращения сжатия.

AndroidManifest.xml

<provider android:authorities="my.app.PdfContentProvider" android:enabled="true" android:exported="true" android:name="my.app.PdfContentProvider"> 
</provider> 

Открытие 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); 

PdfContentProvider.java

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; 
    } 
} 
Смежные вопросы