android新人在线求教如何用tm-extractors-0.4读取word文档 本帖最后由 woyaoxueanzhuo2011 于 2011-03-09 15:50:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果2.2以前/sdcard/drsmart.doc2.2或以后/mnt/sdcard/drsmart.dc text = WordReader.readDoc("/mnt/sdcard/Strust.doc");我改成了这个可是还是无法显示啊。大哥帮帮忙啊。 text = WordReader.readDoc("/mnt/sdcard/assets/Strust.doc");这样也不行啊。有没有大侠帮忙?我的Strust.doc就在asserts下。 InputStream is = getAssets().open("Strust.doc"); int size = is.available(); // Read the entire asset into a local byte buffer. byte[] buffer = new byte[size]; is.read(buffer); is.close(); // Convert the buffer into a string. String text = new String(buffer, "GB2312"); // Finally stick the string into the text view. TextView tv = (TextView) findViewById(R.id.text); tv.setText(text); OK、 楼上,你的办法读出来doc文件是一堆乱码。读txt文件应该没问题。大哥,帮忙啊! /data/data/assets/Strust.doc这样也不行。 Context.getAssets().open(“Strust.doc”) ? 楼上,要得到这个文件的路径并用String显示,这样才可以用jar包中的WordReader.readDoc函数,你给的这个得到当前上下文之后怎么转化啊?求教! 楼主是不是把文件放在项目中的asset文件夹下面啊?如果是的话,用下面语句来读取文件:InputStream is = getAssets().open("Strust.doc");如果是sd卡中的asset文件夹里面:readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/asset/Strust.doc"); 楼上,我是放在assets文件夹下啊!但是你说的InputStream is = getAssets().open("Strust.doc");直接得到了当前文件的内容对吧!我只想要这个Strust文件的路径并以字符串显示而已啊!怎么才能做到?大侠帮忙! WordReader.readDoc("file:///android_asset/Strust.doc");这样试一下 我的意思是你把drsmart.doc放到你sd卡根目录, 楼主用的是真机还是模拟器?如果是真机,直接放到真机的跟目录即可如果是模拟器,打开DDMS,在File Explorer中有sdcard文件夹,有些版本在的sdcard在mnt文件夹下,然后在又上方,有个向右的箭头,点击将文件添加即可(添加的时候注意:1、文件名不能有中文字;2、如果添加不进去,可能sdcard没有权限,在adb命令下,用chmod 777 命令修改权限)读取的话只需要换一下路径即可:String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "struct.doc");其他的代码都一样,我再真机上测试成功了! 楼上huweilong1030大哥,我按照你的方法,打开了file Explorer把文件t.doc成功加入了sdcard目录,但是用如下代码运行结果没有显示任何的东西,不知道错在哪里:String text = null;WordReader wordReader = new WordReader();try { text = wordReader.readDoc(Environment.getExternalStorageDirectory().getAbsolutePath() + "t.doc");} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();} TextView tv = (TextView) findViewById(R.id.myTextView);// tv.setText(text); tv.setText(text);} 我用text = wordReader.readDoc("/mnt/sdcard/t.doc");这样读取的时候,报错,异常终止了,是不是还是jar包冲突啊。是不是tm-extractors-0.4在android中还是不能用? 路径里面少了“/”Environment.getExternalStorageDirectory().getAbsolutePath() 得出的路径是 “/sdcard”所有整个路径应该是 Environment.getExternalStorageDirectory().getAbsolutePath()+“/t.doc” 如果还是报错,你就把log日志贴出来,看报的什么错! 楼上,我加入/之后,代码如下: text = wordReader.readDoc(Environment.getExternalStorageDirectory().getAbsolutePath() + "/t.doc")它和 text = wordReader.readDoc("/mnt/sdcard/t.doc");是一个效果,报错如下:sorry,The application ReadText(process go.read)has stopped unexpectedly.Please try again.楼上,请问错误日志去哪里找啊? 控制台这里没有报错,如下:[2011-03-12 09:38:55 - Search06] Android Launch![2011-03-12 09:38:55 - Search06] adb is running normally.[2011-03-12 09:38:55 - Search06] Performing go.read.ReadTxt activity launch[2011-03-12 09:38:55 - Search06] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'gogooooSearch'[2011-03-12 09:38:58 - Search06] Application already deployed. No need to reinstall.[2011-03-12 09:38:58 - Search06] Starting activity go.read.ReadTxt on device emulator-5554[2011-03-12 09:39:01 - Search06] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=go.read/.ReadTxt } 打开DDMS,里面有个LogCat,选择后点击logv即可看到运行时所以的日志,然后点击logcat的右上方那个带圈圈的E,就可以把错误信息过滤出来了 只有看日志才能解决问题了我这边已经操作成功了:private TextView text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/baojingong.doc"); text.setText(str.trim().replace("\r", "")); } public String readWord(String file){ // 创建输入流读取doc文件 FileInputStream in; String text = null; try { in = new FileInputStream(new File(file)); WordExtractor extractor = null; // 创建WordExtractor extractor = new WordExtractor(); // 对doc文件进行提取 text = extractor.extractText(in); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return text; }就这些代码,文件放在模拟器sd卡的根目录,读出来的效果如下: 我刚刚把这个写了一个文档,你可以去看一下效果,代码就那些,具体报错信息还得看log日志。http://blog.csdn.net/huweilong1030/archive/2011/03/12/6242666.aspx 楼上的老兄,我以前代码和你的差不多,运行之后还是强制关闭。你用的是android3.0吗? 楼上,我去看了你的空间,复制过来的代码运行了之后还是强制关闭。你用的是android3.0吗? 我的错误日志是这样的:03-12 07:59:13.568: ERROR/dalvikvm(3378): Could not find class 'org.textmining.text.extraction.WordExtractor', referenced from method com.word.read.WordReader.readWord 我用的2.2的版本这个错误信息是因为第三方jar包没有找到,可能楼主导包方式出错了在Eclipse里面右键点你的项目选择properties,然后选择library,点击你需要添加的jar包,加进去编译应该就可以了我是用这种方式添加的jar包,你试一下,可以肯定的是jar导入出错了! 请问下楼主有没什么好办法打开和编辑word文档啊?界面能够强大点的插件也行,怎么使用? 楼主,如果word文档中 有图片,如何读取图片呢? Android系统中英文转换 android webview 进度条问题~~~ 关于socket长连接 listView每行有一个删除按钮,点击按钮后,如何获取ItemID app发布到google market上上传图片问题 关于android的国外的新闻 求教關於自動對焦的問題 Android权限问题请教 Android 从网页中跳转到APP 怎么样移除正在运行的栈堆里的某个包? 新手有 问 android文件监控
2.2或以后/mnt/sdcard/drsmart.dc
这样也不行啊。有没有大侠帮忙?我的Strust.doc就在asserts下。
InputStream is = getAssets().open("Strust.doc");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer, "GB2312");
// Finally stick the string into the text view.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(text); OK、
大哥,帮忙啊!
这样也不行。
InputStream is = getAssets().open("Strust.doc");如果是sd卡中的asset文件夹里面:
readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/asset/Strust.doc");
但是你说的InputStream is = getAssets().open("Strust.doc");
直接得到了当前文件的内容对吧!我只想要这个Strust文件的路径并以字符串显示而已啊!怎么才能做到?大侠帮忙!
这样试一下
我的意思是你把drsmart.doc放到你sd卡根目录,
然后在又上方,有个向右的箭头,点击将文件添加即可
(添加的时候注意:1、文件名不能有中文字;2、如果添加不进去,可能sdcard没有权限,在adb命令下,用chmod 777 命令修改权限)读取的话只需要换一下路径即可:String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "struct.doc");
其他的代码都一样,我再真机上测试成功了!
楼上huweilong1030大哥,我按照你的方法,打开了file Explorer把文件t.doc成功加入了sdcard目录,但是用如下代码运行结果没有显示任何的东西,不知道错在哪里:
String text = null;
WordReader wordReader = new WordReader();
try {
text = wordReader.readDoc(Environment.getExternalStorageDirectory().getAbsolutePath() + "t.doc");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView tv = (TextView) findViewById(R.id.myTextView);
// tv.setText(text);
tv.setText(text);
}
text = wordReader.readDoc(Environment.getExternalStorageDirectory().getAbsolutePath() + "/t.doc")
它和
text = wordReader.readDoc("/mnt/sdcard/t.doc");
是一个效果,报错如下:
sorry,
The application ReadText(process go.read)has stopped unexpectedly.Please try again.
楼上,请问错误日志去哪里找啊?
[2011-03-12 09:38:55 - Search06] Android Launch!
[2011-03-12 09:38:55 - Search06] adb is running normally.
[2011-03-12 09:38:55 - Search06] Performing go.read.ReadTxt activity launch
[2011-03-12 09:38:55 - Search06] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'gogooooSearch'
[2011-03-12 09:38:58 - Search06] Application already deployed. No need to reinstall.
[2011-03-12 09:38:58 - Search06] Starting activity go.read.ReadTxt on device emulator-5554
[2011-03-12 09:39:01 - Search06] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=go.read/.ReadTxt }
选择后点击logv即可看到运行时所以的日志,
然后点击logcat的右上方那个带圈圈的E,就可以把错误信息过滤出来了
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/baojingong.doc");
text.setText(str.trim().replace("\r", ""));
}
public String readWord(String file){
// 创建输入流读取doc文件
FileInputStream in;
String text = null;
try {
in = new FileInputStream(new File(file));
WordExtractor extractor = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return text;
}就这些代码,文件放在模拟器sd卡的根目录,读出来的效果如下:
你用的是android3.0吗?
03-12 07:59:13.568: ERROR/dalvikvm(3378): Could not find class 'org.textmining.text.extraction.WordExtractor', referenced from method com.word.read.WordReader.readWord
这个错误信息是因为第三方jar包没有找到,可能楼主导包方式出错了在Eclipse里面右键点你的项目选择properties,然后选择library,点击你需要添加的jar包,加进去编译应该就可以了我是用这种方式添加的jar包,你试一下,可以肯定的是jar导入出错了!