中选择pdf文件的路径。
private static final int DIALOG_LOAD_FILE = 1000;我有两个按钮,一个用于获取pdf文件的路径,另一个用于输出文本:
Button b1 = (Button) x.findViewById(R.id.buttonStripText);
Button button = (Button) x.findViewById(R.id.pick);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,DIALOG_LOAD_FILE);
}
});
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
stripText(v);
}
});另外两个函数是
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case DIALOG_LOAD_FILE:
if(resultCode==RESULT_OK){
fileName = data.getData().getPath();
System.out.println("Your File Name is:::"+fileName);
}
break;
}
}
private void setup() {
PDFBoxResourceLoader.init(getActivity().getApplicationContext());
root = android.os.Environment.getExternalStorageDirectory();
assetManager = getActivity().getAssets();
}
public void stripText(View v) {
String parsedText = null;
try {
PDDocument document = PDDocument.load(assetManager.open("cover_letter.pdf"));
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(1);
parsedText = "Parsed text: " + pdfStripper.getText(document);
if (document != null) document.close();
} catch (Exception e) {
e.printStackTrace();
}
tv.setText(parsedText);
}我没有抛出任何错误,但也没有得到提取的文本。这种Dialog_Load_File打开谷歌驱动器,如果可能的话,向我展示如何打开内部存储!任何帮助都将不胜感激!
发布于 2018-04-27 13:07:04
PDDocument document = PDDocument.load(assetManager.open("cover_letter.pdf"));
PDDocument document = PDDocument.load(... from any input stream .... ); 因此,如果您可以从资产、原始或文件或uri打开输入流,那么就完成了。
例如,如果在onActivityResult中获得uri
InputStream is = getContentResolver().openInputStream(data.getData());
PDDocument document = PDDocument.load( is ); https://stackoverflow.com/questions/50047361
复制相似问题