目标是能够将PDF文件添加到应用程序的“资产”文件夹中,并让用户打开该PDF PDF应用程序(ezPDFReader,Adobe Reader等)。这并非完全简单,因为PDF首先需要将资产文件夹复制到设备存储上。如果设备上没有PDF阅读器应用程序,我们需要显示一条警告消息并指示他们到Play商店下载。如何从Android应用程序打开PDF(在单独的PDF查看器应用程序中)
1
A
回答
6
这是为我工作的解决方案。这是一个名为PdfHandler
的类,下面介绍如何使用它 - 只需将pdf文件放入资源文件夹。如果可用,它将在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;
}
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完美的作品,但它是一个有点慢,因为它复制PDF首先从资产存储它才能打开。如果使用ContentProvider,打开PDF将会更快。
请注意,如果您的文件名是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. android:从我的应用程序打开PDF而不使用pdf查看器
- 2. 在Android应用程序中打开并查看PDF
- 3. 从iOS应用程序打开文件查看器(PDF)
- 4. 黑莓应用程序PDF查看器
- 5. pdf查看器for pyqt4应用程序?
- 6. 从iPhone打开PDF应用程序
- 7. 从其他应用程序打开pdf
- 8. 在Safari上打开PDF应用程序
- 9. 如何从Adobe Reader中的Android应用程序在服务器中打开pdf
- 10. 如何从url查看pdf(在android应用程序中查看选项)?
- 11. 通过Java应用程序查看PDF应用程序
- 12. 要调用的Android PDF查看器应用程序
- 13. 从RCP应用程序的帮助菜单中打开pdf
- 14. 应用程序查看PDF文件
- 15. 从URL打开PDF在Android应用程序
- 16. 的Android,试图Xamarin的Android应用程序中查看PDF
- 17. 如何通过我的Android应用程序打开PDF文件?
- 18. 如何使用PDF库在Android应用程序中呈现PDF
- 19. 在PhoneGap/Android应用程序中查看下载的PDF
- 20. 从Android应用程序中读取PDF
- 21. 在Android中打开的应用程序中查看应用程序图标
- 22. 我无法在我的android应用程序中打开PDF
- 23. 无需启动单独应用程序的Metro风格应用程序的PDF查看器
- 24. PDF打印应用程序服务器
- 25. 在Android应用程序中查看本地PDF文件
- 26. 从我的应用程序打开pdf到另一个pdfviewer应用程序
- 27. 在Xamarin Android应用程序中下载并打开PDF文件?
- 28. 在科尔多瓦应用程序中打开pdf(android)
- 29. ActivityNotFoundException当尝试在Android应用程序中打开PDF文件
- 30. 在Android应用程序中打开PDF文件
+1。很有用。一个很好的增强将是缓存文件的选项,以便它们不需要每次都被复制,在需要更快加载和更少电量消耗的情况下,并且不需要担心持久性存储使用情况。 – Nate 2013-02-16 23:30:14
我很欣赏你的努力。它真的帮助其他人打开pdf外部应用程序。你能否给我建议我如何在没有行动意图的情况下在我的应用程序中打开pdf。 – 2017-04-03 09:35:44