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

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

说明:

现在,该程序从文件管理器访问文件,该文件管理器获取所选文件路径并将其设置为“mFilename”EditText字段。 下面的show PDF按钮监听器显示字符串’pdffilename’被赋予’mFilename’EditText字段中包含的String。 启动PdfViewerActivity并将String’pdffilename’作为Extra传递。 在onCreate()中,如果为null,则检查intent。 这是我认为可以/应该做出改变的地方。 字符串’pdffilename’分配如下所示。 我想要做的是以两种方式之一存储PDF文件…在’res / raw / example_folder / example.pdf’或assets文件夹中。 我想以编程方式为我存储这些PDF文件的路径分配’pdffilename’。 我尝试了许多不同的方法,所有方法都没有编译,导致错误,或导致“文件:res / raw / example_folder / example.pdf不存在!”。

基本上……

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

非常感谢你,我将站在一边回答评论并编辑这篇文章。 我希望这篇文章对其他用户有所帮助,所以我将发布解决方案的代码。 完成后。 再次感谢你!

在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()从上面的show PDF Listener调用…

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

再次感谢你!

要从资产中将其作为输入流访问:

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

经过许多个小时,这里有很多香烟rest的解决方案。 一旦readToByteBuffer返回了ByteBuffer,就像创建一个接收ByteBuffer的新PDFFile一样简单。

请享用…

ShowPDF按钮监听器……

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

从这里编辑了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; }