gpt4 book ai didi

java - Android:读取pdf文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:26 26 4
gpt4 key购买 nike

我想在我的 Android 应用程序中使用 PDFViewer。在 FirstActivity 中,我在 ListView 中加载 pdf 文件,这是代码:

public class MainActivity extends ActionBarActivity {

final StringBuffer sb = new StringBuffer();

private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

String filepath;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);

mainListView = (ListView) findViewById( R.id.mainListView );
final ArrayList<String> List = new ArrayList<String>();

final File storage = Environment.getExternalStorageDirectory();
File file = new File(storage,"/Folder/");

if (file.exists() && file.isDirectory()) {
for (String s : file.list()) {
sb.append(s + " ");

List.addAll( Arrays.asList(s) );
}
}

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView,List);
mainListView.setAdapter( listAdapter );

mainListView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

String filepath = new File("/Folder/"+ List.get(arg2)).getAbsolutePath();

openPdfIntent(filepath);


}


});
}
private void openPdfIntent(String path) {
try {
final Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

SecondActivity 显示 pdf 文件,但出现“找不到文件”的错误,这是代码:

public class SecondActivity extends PdfViewerActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

public int getPreviousPageImageResource() {
return R.drawable.left_arrow;
}

public int getNextPageImageResource() {
return R.drawable.right_arrow;
}

public int getZoomInImageResource() {
return R.drawable.zoom_in;
}

public int getZoomOutImageResource() {
return R.drawable.zoom_out;
}

public int getPdfPasswordLayoutResource() {
return R.layout.pdf_file_password;
}

public int getPdfPageNumberResource() {
return R.layout.dialog_pagenumber;
}

public int getPdfPasswordEditField() {
return R.id.etPassword;
}

public int getPdfPasswordOkButton() {
return R.id.btOK;
}

public int getPdfPasswordExitButton() {
return R.id.btExit;
}

public int getPdfPageNumberEditField() {
return R.id.pagenum_edit;
}
}

如何解决这个问题?

最佳答案

您可以在名为 openPdfIntent() 的函数中使用以下代码来使用设备中的内置应用

示例:

public void openPdfIntent(String filePath){
Uri path = Uri.fromFile(filePath);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
UDF.getdialog("You don't have application for PDF Viewer.",
activity);
}
}

关于java - Android:读取pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29093825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com