【问题标题】:How to render a pdf in android using pdf renderer如何使用 pdf 渲染器在 android 中渲染 pdf
【发布时间】:2022-02-04 05:16:00
【问题描述】:

大家好,如何解决我的错误日志我究竟是不是使用这个特定的类来实现这一点,任何想法都会非常感激。


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)

这是处理 pdf 活动及其显示的活动


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();
    }
    
    
    
}

这是使用意图将 pdf 文件从片段传递到活动的代码。

startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));

这里是 pdf 活动 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);

【问题讨论】:

    标签: java android android-fragments android-intent pdfrenderer


    【解决方案1】:

    startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));

    改为:

    startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsolutePath()));
    

    【讨论】:

      【解决方案2】:

      您可以在此处使用此代码。它对我来说很好用

      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()
      }
      

      }

      【讨论】:

        【解决方案3】:

        好的,谢谢你们的帮助,但我后来发现了问题的答案

        startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));
        

        我实际上并没有返回一个字符串,而是一个返回文件的文件绝对路径,这就是为什么日志说不可能将文件转换为字符串并且代码一直回退到默认字符串值的原因的文件路径为空我的解决方案是通过添加这个将文件绝对路径转换为字符串 startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile().toString())); 这样就完成了。

        【讨论】:

        • 也谢谢你们
        • 我在战斗后发现很晚,但还是谢谢
        猜你喜欢
        • 1970-01-01
        • 2011-10-28
        • 2011-02-22
        • 2019-02-13
        • 1970-01-01
        • 2022-11-17
        • 2016-09-24
        • 2011-01-28
        相关资源
        最近更新 更多