android 打开pdf文件
2016-03-23 13:24
639 查看
android 打开pdf的几种方法
Intent传递文件路径, PDFView
,android-debug.aar
intent 传递
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
这样会有个问题是通过第三方app打开 如果用户手机没有装能打开pdf文件的app 这个PDF就无法打开 用户体验不好
PDFView 在之前项目用了一下 通过自己写activity 和PDFView来打开pdf,缺陷打开pdf支持pdf编码不多
好像只支持UTF-8 而且打开界面后前几秒 画面跟打了马赛克一样,模糊
open_pdf.xml文件
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
java代码
通过aar打开 (android studio)
在build.gradle
Intent传递文件路径, PDFView
,android-debug.aar
intent 传递
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
这样会有个问题是通过第三方app打开 如果用户手机没有装能打开pdf文件的app 这个PDF就无法打开 用户体验不好
PDFView 在之前项目用了一下 通过自己写activity 和PDFView来打开pdf,缺陷打开pdf支持pdf编码不多
好像只支持UTF-8 而且打开界面后前几秒 画面跟打了马赛克一样,模糊
open_pdf.xml文件
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
java代码
import android.app.Activity; import android.os.Bundle; import com.joanzapata.pdfview.PDFView; import com.joanzapata.pdfview.listener.OnPageChangeListener; import java.io.File; public class OpenPDFActivity extends Activity implements OnPageChangeListener { private PDFView pdfView; Integer pageNumber = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.open_pdf); pdfView = (PDFView) findViewById(R.id.pdfView); String path = getIntent().getStringExtra("PATH");// File file = new File(path); display(file, true); } private void display(File assetFileName, boolean jumpToFirstPage) { if (jumpToFirstPage) pageNumber = 1; pdfView.fromFile(assetFileName) .defaultPage(pageNumber) .onPageChange(this) .showMinimap(false) .enableSwipe(true) .load(); } @Override public void onPageChanged(int page, int pageCount) { pageNumber = page; } @Override public void onBackPressed() { super.onBackPressed(); } }
通过aar打开 (android studio)
在build.gradle
compile(name: 'android-debug', ext: 'aar')
java代码
public void skip(String path) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), MuPDFActivity.class); intent.setAction("android.intent.action.VIEW"); File file = new File(path); intent.setData(Uri.fromFile(file)); startActivity(intent); }点击下载arr文件
相关文章推荐
- 这些小工具让你的 Android 开发更高效(上)
- Android之NDK开发
- AndroidStudio更新失败
- Android startActivity源码详解
- Android studio 遇到的问题
- Android课程---布局管理器中的线性布局
- Android开发艺术探索 读书笔记 第三章 View事件体系
- Android推送(长连接)探索
- Android获取Manifest中<meta-data>元素的值
- Android Wear 兼容
- android studio获取发布版安全码
- Android BottomNavigationBar底部导航控制器的使用
- Android根据手机远近自动切换扬声器或听筒
- Android Studio导入项目的遇到的两个问题
- Android有关Service问题汇总
- android开发步步为营之98:android studio使用技巧之一(如何清理不需要的文件)
- android PendingIntent
- Android view绘制流程
- Android中ViewPager实现滑动指示条及与Fragment的配合
- 安卓触屏事件--对象,监听器,属性,行为,多点触摸,MoMotionEvent常用方法