Android - 在外部应用程序中打开pdf

时间:2011-02-26 18:55:07

标签: android pdf android-contentprovider

我在我的app资源目录中有一个pdf文件,我希望使用外部应用程序打开,所以写了我的内容提供商,我试着让它工作但没有...

这是代码:

内容提供商:

package package.name;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileContentProvider extends ContentProvider {
       private static final String URI_PREFIX = "content://package.name.filecontentprovider";

       public static String constructUri(String url) {
           Uri uri = Uri.parse(url);
           return uri.isAbsolute() ? url : URI_PREFIX + url;
       }

       @Override
       public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
           URI uri1 = URI.create("file:///data/data/package.name/"+uri.getPath()); 
           File file = new File(uri1.getPath());
           ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
           return parcel;
       }

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

       @Override
       public int delete(Uri uri, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }

       @Override
       public String getType(Uri uri) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }

       @Override
       public Uri insert(Uri uri, ContentValues contentvalues) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }

       @Override
       public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }

       @Override
       public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }

    }

在这里我如何调用文件的开头:

    File pdf = new File("assets/prova.pdf");
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdf));
    i.setType("application/pdf");
    startActivity(i);

我在标签内的android清单中添加了以下行:

    <provider android:name=".FileContentProvider" android:authorities="package.name" />

这是logcat输出:

02-26 19:47:44.938: ERROR/AndroidRuntime(6494): Uncaught handler: thread main exiting due to uncaught exception
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen}: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.os.Looper.loop(Looper.java:123)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at dalvik.system.NativeStart.main(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): Caused by: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at com.tf.thinkdroid.pdf.app.RenderScreen.onNewIntent(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at com.tf.thinkdroid.pdf.app.RenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at com.tf.thinkdroid.pdf.app.PdfRenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494):     ... 11 more

不明白问题所在,似乎外部应用程序无法获取文件。

感谢您的帮助!

3 个答案:

答案 0 :(得分:5)

我的实施如下。请注意,如果您的文件名是MyPdf.pdf,则该文件应为assets / 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;
  }
}

答案 1 :(得分:2)

setType()将您的Uri重置为null。尝试跳过构造函数中的Uri并改为使用setDataAndType()

答案 2 :(得分:2)

我是Android开发新手,花了一整天时间寻找外部应用程序无法打开本地存储的PDF的原因。我很高兴找到这个帖子。

与此同时,Marco得到了它并在此处进行了描述。 注意:意大利语 - 谷歌翻译服务可能有所帮助; - )

<击> http://www.marcofaion.it/?p=7
http://web.archive.org/web/20111020204554/http://www.marcofaion.it/?p=7

他对初学者的指导的补充说明:

Marco提到要插入Manifest.xml

<provider android:name=".FileContentProvider" android:authorities="package.name" />

应插入 <application ...></application>标记内。

如果你打算有自定义文件名,你应该交换

InputStream is = am.open("file.pdf");

InputStream is = am.open(uri.getLastPathSegment());

PDF文件必须放入项目中已存在的文件夹资产中(特别是在任何新添加的文件夹res / assets或某事物中)! =)