0

可能重复:
如何在 android 中读取 doc 和 excel 文件?

我的 SD 卡上保存了一个 .doc 文件。我需要阅读 .doc 文件的内容并将其显示在 TextView 中。

谁能告诉我该怎么做?

4

3 回答 3

2
public void onCreate(Bundle b){
     super.onCreate(b);
      setContentView(R.layout.main);
      String extPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.Separator;
    InputStream inputStream = assetManager.open(extPath + "file.doc");
    String text = loadFile(inputStream); 
    TextView tv = (TextView)findViewById(R.id.txtv);
    tv.setText(text);



}



public String loadFile(InputStream inputStream){
 ByteArrayOutputStream b = new ByteArrayOutputStream();
 byte[] bytes = new byte[4096];
int length = 0;
  while(){
     b.write(bytes, 0, length);
  }
return new String(b.toByteArray(), "UTF8");
}
于 2012-10-10T10:17:02.680 回答
0

一种解决方案是使用Apache POI Java 库来解析 .doc 文件。

要在 Android 的 SD 卡上获取文件,您可以使用类似

new File(getExternalFilesDir(null), "word.doc");
于 2012-10-10T10:08:42.383 回答
0

试试这个代码

 File file=new File("/sdcard/word.doc");
                        if(file.exists())
                        {
                              Uri path=Uri.fromFile(file);
                              Intent intent=new Intent(Intent.ACTION_VIEW);
                              intent.setDataAndType(path, "application/doc");

                              try
                              {

                                    startActivity(intent);
                              }
                              catch(ActivityNotFoundException e)
                              {
                                    Toast.makeText(TestActivity.this, "No software for Doc", Toast.LENGTH_SHORT).show();
                              }
                        }
于 2012-10-10T10:18:36.643 回答