在我的应用程序中,我想将 pdf 文件转换为文本文件,任何人都可以提供帮助。
从 sdcard 读取 pdf 文件并将其转换为文本文件并存储在 sd 卡上。
问问题
5629 次
2 回答
2
于 2013-01-28T07:35:34.537 回答
1
从 sd 卡读取 pdf 文件:
Please check your device is any pdf reader application available, I think isn't any..
只需使用此代码,
private void viewPdf(Uri file) {
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// No application to view, ask to download one
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("No Application Found");
builder.setMessage("Download one from Android Market?");
builder.setPositiveButton("Yes, Please",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent
.setData(Uri
.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
}
}
If any pdf reader application not available then this code is download pdf reader from android market, But be sure your device has pre-installed **android-market** application. So I think try this on android device, rather then emulator.
将pdf文件转换为文本文件
可以将pdf转换为文本文件。如果您的 pdf 文件作为图像,您想使用 OCR 工具。
还有一套解析库和pdf的sdk。您可以查看此 https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android。
将文件存储在 sdcard 上
btnWriteSDFile.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
txtData.setText("");
}
catch (Exception e)
{
Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
btnReadSDFile.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null)
{
aBuffer += aDataRow ;
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(v.getContext(),"Done reading SD 'mysdfile.txt'",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
除了这个,还要在 Android.Manifest 中写下这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-01-28T07:32:45.807 回答