使用第三方应用程序从sdcard读取.docx文件

时间:2012-12-21 11:32:00

标签: android

我是android新手。在我的应用程序中,我需要阅读.doc和.docx以及其他办公套装文件。视他们的存在而定。我想在我的应用程序中使用Intent阅读它。 我尝试了很多代码,但它们无法正常工作。 我的代码如下所示使用各种模块。我应该做什么改变或者有没有其他方法来阅读这些文件?

我的代码是:

/**
     *  Apache OPI Code.
     */
    if(Environment.getExternalStorageState().equalsIgnoreCase("y")){
        TextView tv = (TextView) findViewById(R.id.textview_data_rla);
        WordExtractor extractor = null;
        try {
           File file = new File(getExternalFilesDir(null),"n.doc");
           FileInputStream fis=new FileInputStream(file.getAbsolutePath());
           Log.d("File", fis.toString());
           HWPFDocument document=new HWPFDocument(fis);
           extractor = new WordExtractor(document);
           String[] fileData = extractor.getParagraphText();
           for(int i=0;i<fileData.length;i++){
             if(fileData[i] != null){
               tv.setText(fileData[i]);
               Log.d("file",fileData[i]);
             }
           }
        }
        catch(Exception exep){

        }
    }

    /**
     *  Using HWPFDocument.
     */

    try {
        TextView tv = (TextView) findViewById(R.id.textview_data_rla);

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard,"n.doc");

        //InputStream fis = new FileInputStream(file);
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));  
        HWPFDocument doc = new HWPFDocument(fs);  

        Range range = doc.getRange();  
        CharacterRun run = null;
        for (int i=0; i<range.numCharacterRuns(); i++) {  
            run = range.getCharacterRun(i);  
            Log.d("character  run",String.valueOf(i+1));
            Log.d("Text",run.text().toString());
        }
        tv.setText(run.text().toString());
        OutputStream out = new FileOutputStream(new File("/mnt/sdcard/new.doc"));
        doc.write(out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  

    /**
     *  Apache POI using NPOIFSFileSystem.
     */
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"n.doc");
    WordExtractor extractor1 = null;
    try{
        NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
        extractor1 = new WordExtractor(fs.getRoot(), null);
    } catch (Exception e){
        e.printStackTrace();
    }

    for(String rawText : extractor1.getParagraphText()) {
    String text = WordExtractor.stripFields(rawText);
    Log.d("text",text);
    }

    /**
     *  Apache Tika code.
     */

    TikaConfig tika = TikaConfig.getDefaultConfig();
    TikaInputStream stream = TikaInputStream.get(file);
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    tika.getParser().parse(file, handler, metadata, new ParseContext());
    String text = handler.toString();


    /**
     *  Code using jOpenDocument.jar
     */

    final OpenDocument doc = new OpenDocument();
    doc.loadFrom("invoice.ods");

    // Show time !
    final JFrame f = new JFrame("Invoice Viewer");
    ODSViewerPanel viewer = new ODSViewerPanel(doc);
    f.setContentPane(viewer);
    f.pack();
    f.setVisible(true);
}

我必须使用哪种方法。

请尽快回复。 提前谢谢。

0 个答案:

没有答案