从“RES /原始”访问PDF文件或文件夹的资产与编程给出的方法解析 [英] Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods

查看:135
本文介绍了从“RES /原始”访问PDF文件或文件夹的资产与编程给出的方法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这个程序访问从接受选定文件的路径和它设置为mFilename的EditText领域的文件管理器的文件。下面展示的PDF按钮,监听器显示字符串'pdffilename'被分配载于mFilename的EditText字段中的String。该PdfViewerActivity开始和字符串'pdffilename'被作为额外的传递。在OnCreate()的目的是检查是否空或不是。这是我认为的改变可以/应。字符串'pdffilename'分配你看到下面的东西。我想要做的是两种方式......一个PDF文件存储的int'RES /生/ example_folder /为例.pdf或资产的文件夹中。我要分配pdffilename编程与我在哪里存储这些PDF文件的路径。我已经尝试了许多不同的方法全部都既没有编制,造成失误或造成文件:RES /生/ example_folder /为例.pdf不存在。

Right now this program accesses a file from a file manager that takes the selected files path and sets it to the 'mFilename' EditText field. The show PDF button listener below shows the String 'pdffilename' gets assigned the String contained in the 'mFilename' EditText field. The PdfViewerActivity is started and the String 'pdffilename' is passed as an Extra. In the onCreate() the intent is checked if null or not. This is where I think the change can/should be made. The String 'pdffilename' is assigned what you see below. What I want to do is store the PDF files in one of two ways... int the 'res/raw/example_folder/example.pdf' or in the assets folder. I want to assign 'pdffilename' programmatically with the path for where I am storing these PDF files. I have tried many different approaches all of which have either did not compile, caused errors, or caused a "file: res/raw/example_folder/example.pdf does not exist!".

所以基本上...


  • 我要PDF文件存储在RES /生/ folder_example /为例.pdf或资产的文件夹

  • 我想从code访问这些文件在我不需要使用文件管理器

  • 总之,这将解决这将是最大的帮助,我是pretty好与Java,但我绝不是一个超级明星,所以请解释一下你的code

感谢您这么多,我会站在回答评论和编辑这个职位。我希望这篇文章将有助于其他用户,所以我将张贴在code的解决方案。当完成。再次感谢您!

Thank you so much and I will be standing by to answer comments and edit this post. I hope this post will be helpful to other users so I will be posting the code for the solution. when completed. Thank you again!

在PdfFileSelectActivity 显示PDF按钮,监听器...

Show PDF button Listener in PdfFileSelectActivity...

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;

   }

再次感谢您!

推荐答案

很多很多的时间和相当多的香烟在这里休息后是解决方案。一旦readToByteBuffer返回到ByteBuffer它是作为创建一个新的PDFFile是发生在一个ByteBuffer中那样容易。

After many many hours and quite a few cigarette breaks here is the solution. Once the readToByteBuffer has returned a ByteBuffer it is as easy as creating a new PDFFile that takes in a ByteBuffer.

享受...

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

edited readToByteBuffer() from here

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

这篇关于从“RES /原始”访问PDF文件或文件夹的资产与编程给出的方法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆