在浏览项目的时候,发现有这么一个不错的工具【android-pdfview】,直接可以像浏览txt文档一样浏览pdf文档,这里我贴一下代码,主要是给自己标记一下。
在布局文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/a1_head" />
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444" />
</LinearLayout>
然后在activity中:
public class PdfViewActivity extends BaseActivity implements OnPageChangeListener{
private PDFView pdfView;
private int pageNum=1;
private String filePath = Environment.getExternalStorageDirectory()
.getPath() + "/myTest.pdf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_view);
copyFileToSdcard();
setActivityHeaderTitle(R.string.data_activity_name);
getHeaderLeftButton().setImageResource(R.drawable.icon_back);
File file=new File(filePath);
pdfView=(PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(file)
.defaultPage(pageNum)
.onPageChange(PdfViewActivity.this)
.load();
}
@Override
public void onPageChanged(int page, int pageCount) {
page=pageNum;
}
private void copyFileToSdcard() {
InputStream inputstream = getResources().openRawResource(
R.raw.ceshi);
byte[] buffer = new byte[1024];
int count = 0;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filePath));
while ((count = inputstream.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Toast.makeText(PdfViewActivity.this, "Check your sdcard", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}