如何使用 pdf 渲染器在 android 中渲染 pdf
How to render a pdf in android using pdf renderer
大家好,如何解决我的错误日志我正在尝试使用 android 中的 android 图形 pdfrenderer class 渲染 pdf 我试图观察在哪里或如何我弄错了,但我还看不到,请各位,我究竟是如何使用这个特定的 class 实现这一目标的?任何想法都将不胜感激。
Shared attribute region not available to be mapped
02-04 05:35:25.750 15257 15257 W ActivityThread handleWindowVisibility: no activity for token android.os.BinderProxy@cb509ee
02-04 05:35:25.936 15257 15257 W Bundle Key filePath expected String but value was a java.io.File. The default value <null> was returned.
02-04 05:35:25.950 15257 15257 W Bundle Attempt to cast generated internal exception:
02-04 05:35:25.950 15257 15257 W Bundle java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
02-04 05:35:25.950 15257 15257 W Bundle at android.os.BaseBundle.getString(BaseBundle.java:1170)
02-04 05:35:25.950 15257 15257 W Bundle at android.content.Intent.getStringExtra(Intent.java:7907)
02-04 05:35:25.950 15257 15257 W Bundle at com.jaay.docReader.PdfActivity.onCreate(PdfActivity.java:26)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Activity.performCreate(Activity.java:7873)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Activity.performCreate(Activity.java:7861)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1312)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3331)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3533)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
这是 activity 处理 pdf activity 和它的显示
public class PdfActivity extends AppCompatActivity {
String filePath = "";
ImageView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
pdfView = (ImageView) findViewById(R.id.pdfview);
filePath = getIntent().getStringExtra("filePath");
File file = new File(filePath);
try{
openPDF(file);
}catch(IOException ioe){
ioe.printStackTrace();
}
//File file = new File(filePath);
//Uri uri = Uri.fromFile(file);
//pdfView.fromUri(uri).load();
}
public void openPDF(File file) throws IOException {
// File file = new File(filePath);
ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);
final int pageCount = pdfRenderer.getPageCount();
Toast.makeText(this,
"pageCount = " + pageCount,
Toast.LENGTH_LONG).show();
//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
pdfView.setImageBitmap(bitmap);
rendererPage.close();
pdfRenderer.close();
fileDescriptor.close();
}
}
这是使用从片段到 activity 的意图传递 pdf 文件的代码。
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
这是 pdf activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_pdf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="jaa"
android:textStyle="bold"/>
<ImageView
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我哪里出错了,有什么奇怪的地方
filePath = getIntent().getStringExtra("filePath");
File file = new File(filePath);
在那里你可以使用这个代码。它对我来说很好
fun getPdfThumbnail(context: Context, file:File, imageView: ImageView, pageCountTV:TextView) {
// create a new renderer
try {
val pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
val renderer = PdfRenderer(pfd)
val bitmap = Bitmap.createBitmap(900, 900, Bitmap.Config.ARGB_4444)
val page = renderer.openPage(0)
val count = renderer.pageCount
pageCountTV.text = "$count pages"
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
Glide.with(context).load(bitmap).into(imageView)
page.close()
renderer.close()
}
catch (e:Exception){
e.printStackTrace()
}
}
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
更改为:
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsolutePath()));
好的,谢谢你们的帮助,但后来我找到了我的问题的答案,事情在
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
我实际上并没有返回一个字符串,而是一个返回文件的文件绝对路径,这就是为什么日志说不可能将文件转换为字符串并且代码一直回落到默认值的原因文件路径的字符串值为 null 我的解决方案是通过添加此文件将文件绝对路径转换为字符串
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile().toString()));
完成了工作。
大家好,如何解决我的错误日志我正在尝试使用 android 中的 android 图形 pdfrenderer class 渲染 pdf 我试图观察在哪里或如何我弄错了,但我还看不到,请各位,我究竟是如何使用这个特定的 class 实现这一目标的?任何想法都将不胜感激。
Shared attribute region not available to be mapped
02-04 05:35:25.750 15257 15257 W ActivityThread handleWindowVisibility: no activity for token android.os.BinderProxy@cb509ee
02-04 05:35:25.936 15257 15257 W Bundle Key filePath expected String but value was a java.io.File. The default value <null> was returned.
02-04 05:35:25.950 15257 15257 W Bundle Attempt to cast generated internal exception:
02-04 05:35:25.950 15257 15257 W Bundle java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
02-04 05:35:25.950 15257 15257 W Bundle at android.os.BaseBundle.getString(BaseBundle.java:1170)
02-04 05:35:25.950 15257 15257 W Bundle at android.content.Intent.getStringExtra(Intent.java:7907)
02-04 05:35:25.950 15257 15257 W Bundle at com.jaay.docReader.PdfActivity.onCreate(PdfActivity.java:26)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Activity.performCreate(Activity.java:7873)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Activity.performCreate(Activity.java:7861)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1312)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3331)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3533)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
02-04 05:35:25.950 15257 15257 W Bundle at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
这是 activity 处理 pdf activity 和它的显示
public class PdfActivity extends AppCompatActivity {
String filePath = "";
ImageView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
pdfView = (ImageView) findViewById(R.id.pdfview);
filePath = getIntent().getStringExtra("filePath");
File file = new File(filePath);
try{
openPDF(file);
}catch(IOException ioe){
ioe.printStackTrace();
}
//File file = new File(filePath);
//Uri uri = Uri.fromFile(file);
//pdfView.fromUri(uri).load();
}
public void openPDF(File file) throws IOException {
// File file = new File(filePath);
ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);
final int pageCount = pdfRenderer.getPageCount();
Toast.makeText(this,
"pageCount = " + pageCount,
Toast.LENGTH_LONG).show();
//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
pdfView.setImageBitmap(bitmap);
rendererPage.close();
pdfRenderer.close();
fileDescriptor.close();
}
}
这是使用从片段到 activity 的意图传递 pdf 文件的代码。
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
这是 pdf activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_pdf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="jaa"
android:textStyle="bold"/>
<ImageView
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我哪里出错了,有什么奇怪的地方
filePath = getIntent().getStringExtra("filePath");
File file = new File(filePath);
在那里你可以使用这个代码。它对我来说很好
fun getPdfThumbnail(context: Context, file:File, imageView: ImageView, pageCountTV:TextView) {
// create a new renderer
try {
val pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
val renderer = PdfRenderer(pfd)
val bitmap = Bitmap.createBitmap(900, 900, Bitmap.Config.ARGB_4444)
val page = renderer.openPage(0)
val count = renderer.pageCount
pageCountTV.text = "$count pages"
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
Glide.with(context).load(bitmap).into(imageView)
page.close()
renderer.close()
}
catch (e:Exception){
e.printStackTrace()
}
}
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
更改为:
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsolutePath()));
好的,谢谢你们的帮助,但后来我找到了我的问题的答案,事情在
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
我实际上并没有返回一个字符串,而是一个返回文件的文件绝对路径,这就是为什么日志说不可能将文件转换为字符串并且代码一直回落到默认值的原因文件路径的字符串值为 null 我的解决方案是通过添加此文件将文件绝对路径转换为字符串
startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile().toString()));
完成了工作。