【问题标题】:How to read .doc file in SD card in Android? [duplicate]如何在 Android 中读取 SD 卡中的 .doc 文件? [复制]
【发布时间】:2012-10-10 10:02:30
【问题描述】:

可能重复:
how to read doc and excel file in android?

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

谁能告诉我怎么做?

【问题讨论】:

标签: android ms-word doc


【解决方案1】:
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");
}

【讨论】:

    【解决方案2】:

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

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

    new File(getExternalFilesDir(null), "word.doc");
    

    【讨论】:

    • 如何解析.docx文件?
    【解决方案3】:

    试试这个代码

     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();
                                  }
                            }
    

    【讨论】:

    • 您好先生,此代码无法启动活动。
    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多