【问题标题】:Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods以编程方式从“res/raw”或 assets 文件夹访问 PDF 文件以使用给定的方法进行解析
【发布时间】:2012-06-16 17:32:13
【问题描述】:

以编程方式从“res/raw”或 assets 文件夹访问 PDF 文件以使用给定的方法进行解析

解释:

现在这个程序从文件管理器中访问一个文件,该文件管理器采用选定的文件路径并将其设置为“mFilename”EditText 字段。下面的显示 PDF 按钮侦听器显示字符串“pdffilename”被分配了包含在“mFilename”EditText 字段中的字符串。 PdfViewerActivity 已启动,字符串“pdffilename”作为 Extra 传递。在 onCreate() 中,检查意图是否为空。这是我认为可以/应该做出改变的地方。字符串 'pdffilename' 分配了您在下面看到的内容。我想要做的是以两种方式之一存储 PDF 文件......在“res/raw/example_folder/example.pdf”或资产文件夹中。我想用我存储这些 PDF 文件的路径以编程方式分配“pdffilename”。我尝试了许多不同的方法,所有这些方法要么无法编译,要么导致错误,要么导致“文件:res/raw/example_folder/example.pdf 不存在!”。

所以基本上...

  • 我想将 PDF 文件存储在“res/raw/folder_example/example.pdf”或 assets 文件夹中
  • 我想从代码中访问这些文件,因为我不需要使用文件管理器
  • 无论如何解决这个问题将是最大的帮助,我对 Java 很擅长,但我绝不是超级明星,所以请用你的代码解释一下

非常感谢您,我将随时回答 cmets 并编辑此帖子。我希望这篇文章对其他用户有所帮助,因此我将发布解决方案的代码。完成时。再次感谢!

在 PdfFileSelectActivity 中显示 PDF 按钮侦听器...

OnClickListener ShowPdfListener = new OnClickListener()
{
    public void onClick(View v)
    {
        mFilename = (EditText) findViewById(R.id.filename);
        String pdffilename = mFilename.getText().toString();
        Intent intent = new Intent(PdfFileSelectActivity.this,
        PdfViewerActivity.class)
        .putExtra(EXTRA_PDFFILENAME, pdffilename);
        startActivity(intent);
    }
};

PdfViewerActivity 的 onCreate() 从上面的显示 PDF 侦听器中调用...

Intent intent = getIntent();
 
if (intent != null)
{
    if ("android.intent.action.VIEW".equals(intent.getAction()))
    {
        pdffilename = storeUriContentToFile(intent.getData());
    }
    else {
        pdffilename = getIntent().getStringExtra(PdfFileSelectActivity.EXTRA_PDFFILENAME);
    }
}

if (pdffilename == null)
    pdffilename = "no file selected";

setContent(null);

setContent() 从上面调用(如果需要)...

private void setContent(String password)
{
    try {
        parsePDF(pdffilename, password);
    }
    catch (PDFAuthenticationFailureException e)
    {
        System.out.println("Password needed");
    }
}

parsePDF() 从上面调用(如果需要)...

    private void parsePDF(String filename, String password) throws PDFAuthenticationFailureException
    {
        long startTime = System.currentTimeMillis();
        try {
            File f = new File(filename);
            long len = f.length();
            if (len == 0) {
                mGraphView.showText("file '" + filename + "' not found");
            }
            else {
                mGraphView.showText("file '" + filename + "' has " + len + " bytes");
                openFile(f, password);
            }
        }
        catch (PDFAuthenticationFailureException e)
        {
            throw e;
        } catch (Throwable e) {
            e.printStackTrace();
            mGraphView.showText("Exception: "+e.getMessage());
        }
        long stopTime = System.currentTimeMillis();
        mGraphView.fileMillis = stopTime-startTime;

   }

再次感谢您!

【问题讨论】:

    标签: java android pdf android-resources android-assets


    【解决方案1】:

    要将其作为来自资产的输入流进行访问:

    in = new BufferedReader(new InputStreamReader(activity.getAssets().open(yourfile.pdf)));
    

    【讨论】:

      【解决方案2】:

      经过许多小时和相当多的香烟休息后,这里是解决方案。一旦 readToByteBuffer 返回了一个 ByteBuffer,它就像创建一个接收 ByteBuffer 的新 PDFFile 一样简单。

      享受...

      显示PDF按钮监听器...

      OnClickListener ShowPdfListener = new OnClickListener() {
          public void onClick(View v)
          {
              Intent intent = new Intent(PdfFileSelectActivity.this,
              PdfViewerActivity.class);
              startActivity(intent);
          }
      };
      

      在 onCreate() PdfViewerActivity...

      openFile2(readToByteBuffer(this.getAssets().open("test.pdf")), null);
      

      here编辑了readToByteBuffer()

      public ByteBuffer readToByteBuffer(InputStream inStream) throws IOException
      {
          long startTime = System.currentTimeMillis();
          BufferedReader in = new BufferedReader(new InputStreamReader(this.getAssets().open("test.pdf")));
          StringBuilder total = new StringBuilder();
          String line;
          while ((line = in.readLine()) != null) {
              total.append(line);
          }
      
          int length = total.length();
          byte[] buffer = new byte[length];
          ByteArrayOutputStream outStream = new ByteArrayOutputStream(length);
          int read;
          while (true) {
            read = inStream.read(buffer);
            if (read == -1)
              break;
            outStream.write(buffer, 0, read);
          }
          ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
          long stopTime = System.currentTimeMillis();
          mGraphView.fileMillis = stopTime-startTime;
          return byteData;
        }
      

      【讨论】:

      • #CommonKnowledge 您正在使用哪个 pdfreader?你在做什么 openFile2(readToByteBuffer(this.getAssets().open("test.pdf")), null);
      • 看起来他正在使用 apv 或 Android PDF 查看器:code.google.com/p/apv 但是 openFile2 似乎是他自己的方法。我猜它需要一个 ByteBuffer 并以某种方式将其转换为 PDFFile 对象......
      • 我的意思是它很容易将字节缓冲区转换为 PDF 文件,但我不知道他用它做什么
      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2012-06-24
      • 2015-03-09
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多