现在,该程序从文件管理器访问文件,该文件管理器获取所选文件路径并将其设置为“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不存在!”。
基本上......
非常感谢你,我将站在一边回答评论并编辑这篇文章。我希望这篇文章对其他用户有所帮助,所以我将发布解决方案的代码。完成后。再次感谢你!
在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);
}
};
从上面的show PDF Listener调用了PdfViewerActivity的onCreate()
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;
}
再次感谢您!
答案 0 :(得分:1)
要将其作为资产的输入流进行访问:
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(yourfile.pdf)));
答案 1 :(得分:1)
经过许多小时,这里有很多香烟休息的解决方案。一旦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);
从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;
}