【发布时间】:2013-11-23 03:27:14
【问题描述】:
当我尝试从 sdcard 读取 pdf 文件并从中提取文本时,什么也没发生。 没有错误,没有警告,通知,也没有结果文件。 我将源文件和结果都存储在设备 sdcard 的根文件夹中。 你们能帮我解决这个问题吗? 这是我的代码:
package com.example.androidtest;
import java.io.File;
...
public class MainActivity extends Activity {
private Button button;
public static final String TIMETABLE = "doc.pdf"; // The original PDF that will be parsed.
public static final String RESULT = "timetable.txt"; // The text file received after scan.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
processSource();
}
public void processSource() {
button = (Button) this.findViewById(R.id.button_add);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new MainActivity().extractText(TIMETABLE, RESULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void extractText(String pdf, String doc) throws IOException {
File sdcard = Environment.getExternalStorageDirectory(); // Load file timetable.txt from device's sdcard
File file = new File(sdcard, pdf);
File text = new File(sdcard, doc); // Save the result file in device's sdcard
InputStream is;
try {
is = new FileInputStream(file);
PdfReader reader = new PdfReader(is); // Call the source file
PrintWriter out = new PrintWriter(new FileOutputStream(text));
Rectangle rect = new Rectangle(0, 0, 600, 900); // Define the rectangle to extract text within it
RenderFilter filter = new RegionTextRenderFilter(rect);
TextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
out.println(PdfTextExtractor.getTextFromPage(reader, 1, strategy));
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Call the source file
}
}
这是我在 AVD 上测试时在控制台选项卡中显示的内容(我希望它可以提供帮助):
[2013-11-23 03:03:29 - AndroidTest] Android 发布! [2013-11-23 03:03:29 - AndroidTest] adb 运行正常。 [2013-11-23 03:03:29 - AndroidTest] 执行 com.example.androidtest.MainActivity >activity 启动 [2013-11-23 03:03:29 - AndroidTest] 自动目标模式:使用 > 兼容的 AVD 'Tab' 启动新模拟器 [2013-11-23 03:03:29 - AndroidTest] 使用虚拟设备“选项卡”启动新模拟器 [2013-11-23 03:03:29 - AndroidTest] 发现新模拟器:emulator-5554 [2013-11-23 03:03:29 - AndroidTest] 等待 HOME ('android.process.acore') >launched... [2013-11-23 03:03:57 - AndroidTest] HOME 在设备 'emulator-5554' 上启动 [2013-11-23 03:03:57 - AndroidTest] 将 AndroidTest.apk 上传到设备 'emulator-5554' [2013-11-23 03:04:06 - AndroidTest] 安装 AndroidTest.apk... [2013-11-23 03:04:29 - AndroidTest] 成功! [2013-11-23 03:04:29 - AndroidTest] 在设备模拟器 5554 上启动活动 >com.example.androidtest.MainActivity [2013-11-23 03:04:30 - AndroidTest] ActivityManager: 开始: Intent >{ act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] >cmp=com.example.androidtest/ .MainActivity }
感谢您的宝贵时间!
【问题讨论】: