显示一个微软Word文件的视图(TextView的说)在Android中 [英] Displaying a MS Word File in View(say TextView) in Android

查看:342
本文介绍了显示一个微软Word文件的视图(TextView的说)在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示 .DOCX 文件中以查看 Android中。该文件在文字之间的数学符号,也图像。我想显示很多这样的文件,并通过刷卡的姿态通过他们翻转。我已经成功地做​​了相同的 .TXT 文件。而现在可以很容易地进入下一个页面上刷卡。在$ C $下的 .TXT 文件如下:

I want to display a .docx file in a View in Android. The file has mathematical symbols and also images in between the text. I want to display many such files and flip through them via swipe gesture. I have successfully done the same for .txt files. And can now very easily go to the next page on swipe. The code for .txt file is as follows:

public String readTxt(String fileName)
    {


        try {
            InputStream is;
            is = context.getAssets().open(fileName + ".txt");
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            int i;
            i = is.read();
            while (i != -1)
            {
                byteArrayOutputStream.write(i);
                i = is.read();
            }

            is.close();

            return byteArrayOutputStream.toString();

        } 

        catch (IOException e) 
        {
            e.printStackTrace();
        }

        return fileName;
    }

这code返回,然后我在显示我的的TextView 的文本。这样,我只需要动态和轻扫更改文件名的文件进行修改。

This code returns the text which I then display in my TextView. This way I only need to change the name of the file dynamically and on swipe the Text changes.

现在我要修改此code,这样我可以显示MS Word文件(.DOCX),它包含文本,图像和数学符号。

Now I want to modify this code so that I can display MS Word files(.docx) which contain text,images and mathematical symbols.

我已经检查对堆栈溢出,这个话题很多类似的线程,以及在其他论坛: 这是很多人都认为是回答类似的问题的联系,我已经给了这些一试:<一href="http://stackoverflow.com/questions/12816281/how-to-read-doc-and-excel-file-in-android">Link1和链接2

I have already checked many similar threads on this topic on stack overflow as well as on other forums: These are the links many people have suggested as answers to similar questions and I have already given these a try: Link1 and link2

还有很多其他线程人都推荐的 Jopendocument 。我也看到有关得知Android不支持开放文档格式。这样的选择似乎不太可能。但是,如果您有任何解决方法或相对于一个好的详细的说明添加JOpenDocument库项目和显示丰富的文本,那么请分享解决方案,因为我已经寻找了很多,但找不到任何。

Also on many other threads people have recommended Jopendocument. I have also read about that and learnt that Android does not support open document format. so that option seems unlikely. But if you have any workaround or a good detailed explanation with respect to adding the JOpenDocument library to the project and displaying rich text then please share that solution because I have searched for it a lot but couldn't find any.

还有一个名为 OliveDocLibrary 可显示丰富的词在Android上文件的另一个库。这里是从我下载的lib的链接。包括在下载包中的演示工作只是fine.But的lib是一个试用版。所以,我目前正试图与此库的工作,看看哪里就有奇迹。但我仍然在寻找更好的选择。

There is also another library called OliveDocLibrary to display rich word files on android. here is the link from where I downloaded the lib. The demo included in that download package works just fine.But the lib is a trial version. So am currently trying to work with this library and see where it goes. But am still in search of better options.

任何有关此帮助AP preciated。除了上面提到的以外任何指针是非常受欢迎的。

Any help regarding this is appreciated. Any pointers other than the ones mentioned above are most welcome.

更新:

我有一个建议,这对使用的的Apache POI (HWPF更具体)在第一赏金我开始在这个问题上。探索的Apache POI一段时间后,我得到了一些codeS这被写入一个doc文件,读取doc文件,更新Excel工作表等。

I got a suggestion which told the use of Apache POI(HWPF more specifically) in the first bounty I started on this question. After exploring Apache POI for some time, I got few codes which were writing into a doc file, reading the doc file, updating the excel sheets etc.

这样的样品code(用于Java),我发现了互联网是这样的:

Such Sample code(for Java) which I found off the internet goes something like this:

import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class ReadDocFile {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {

file = new File("c:\\New.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
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)
System.out.println(fileData[i]);
}
}
catch(Exception exep){}
}
}

于是我加了这个库(Apache的POI)我的Andr​​oid项目在Eclipse和尝试这个示例code有了一些变化。试穿了一个的TextView 显示它。这里的问题是,虽然它并不像显示图像的 OliveDocLibrary 一样。所以,如果有人要建议的Apache POI,那我请求了坚实的指针或code可以阅读 DOCX 文件和所有及其内容(包括图片)和一个自定义视图显示出来。

So I added this library(Apache POI) to my Android project in eclipse and tried this sample code with some changes. And tried displaying it on a TextView. The problem here though is it doesn't display the images like OliveDocLibrary does. So if Someone is going to suggest Apache POI, then I request for a solid pointer or a code which reads a docx file and all its contents(that includes images) and displays them in a custom view.

的Apache POI 是一个伟大的事情,但遗憾的是我没有找到实现这些库的任何好的例子/样本。如果你知道的一个例子的良好来源(WRT MS Word只),那么,请在评论中分享他们。

Apache POI is a great thing but sadly I didn't find any good examples/samples implementing those libraries. If you know a good source of examples(w.r.t MS word only) then please share them in comments.

更新2

OliveDocLibrary 包code提供优良工程。有橄榄的水迹上的查看,虽然。目前,我正在上code执行刷卡。但问题仍然是它的试用版。

In OliveDocLibrary package the code provided works fine. There is water mark of Olive on the View though. Currently am working on performing Swipe on that code. But the problem remains that its a trial version.

更新3:

我觉得 OliveDocLibrary 是最有效的方法来做到这一点。虽然有被试用版劣势,我认为没有其他的库做比这个库完全填补我的具体要求更好的工作。详细的答案已经发布以下 。作为赏金时间约为克服。我会请谁可能有另一种更好的解决方案,以尽快发布它的人。现在我要和 OliveDocLibrary ,并接受我自己的答案。

I think OliveDocLibrary is the most efficient way to do it. Though it has a disadvantage of being a trial version, I think no other library does a better job than this library to full fill my specific requirement. The detailed answer has been posted below. As the bounty time is about to get over. I would request the people who may have an alternate and better solution to post it as soon as possible. For now am going with OliveDocLibrary and accepting my own answer.

推荐答案

好家伙经过再三考虑,看着地狱很多,我认为选择和解决方法 OliveDocLibrary 是做到这一点的最好办法。这里是链接这将给指引你到三个的下载页面Android的这对DOC,XLS和PPT库。所有这些工作很好很好。您将下载包文件夹里面有三个文件夹。它们是:

Hello guys After much consideration and looking at hell lot of options and workarounds I think OliveDocLibrary is the best way to do it. Here is the link which will give direct you to the downloads page of three libraries for Android which are for DOC, XLS and PPT. All these work excellently well. The package folder you will download will have three folders inside. which are:

  1. API
  2. lib_trial
  3. 演示

演示文件夹,你会发现一个字一个样本项目。您可以在此项目直接导入到Eclipse中的工作空间和测试code自己。为了方便人们在这里张贴的code。我已删除的code一些部分我觉得是没有必要的(这里WRT的回答我的问题)。因此,code有两个文件,​​其主要活动是文件选择器这是如下:

In the demo folder you will find a sample project for Word. You can directly import this project into your workspace in Eclipse and test the code yourself. For peoples convenience am posting that code here. I have deleted some part of the code which I felt was not necessary(w.r.t the answer to my question here). So the code has two files, The main activity is FileChooser which is as follows:

public class FileChooser extends Activity {

    private String filePath = Environment.getExternalStorageDirectory()
            .getPath() + "/simple.docx";
    MyBaseAdapter adapter;
    private static String parentPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(2);
        copyFileToSdcard();
        Intent intent = new Intent(FileChooser.this,
                OliveWordTrailDemoAcitivy.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.fromFile(new File(filePath)));
        startActivity(intent);
    }

    class MyBaseAdapter extends BaseAdapter {
        private String[] list;

        public MyBaseAdapter(String[] list) {
            this.list = list;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(FileChooser.this);
                ((TextView) convertView).setTextSize(35);
            }
            ((TextView) convertView).setText(list[position]);
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            return list.length;
        }

        public void setList(String[] list) {
            this.list = list;
        }
    };

    class MyItemClickListener implements OnItemClickListener {
        String[] list;
        InputStream is;

        public MyItemClickListener(String[] list) {
            this.list = list;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            File file = new File(parentPath + list[position]);
            if (file.isFile()) {
                Intent intent = new Intent(FileChooser.this,
                        OliveWordTrailDemoAcitivy.class);
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.fromFile(file));
                startActivity(intent);
            } else {
                list = file.list();
                adapter.setList(list);
                adapter.notifyDataSetChanged();
                parentPath = file.getAbsolutePath() + "/";
            }
        }

    }
    private void copyFileToSdcard() {
        InputStream inputstream     = getResources().openRawResource(
                R.raw.simple);
        byte[] buffer = new byte[1024];
        int count = 0;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(filePath));
            while ((count = inputstream.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            fos.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
            Toast.makeText(FileChooser.this, "Check your sdcard", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

在这里,我已经放在一个名为doc文件的 simple.docx ,其中包括图像和它们呈现并正确显示数学符号。与 OliveWordTrialDemoActivity 此活动相互作用而如下:

Here I have placed a doc file named simple.docx which includes images and mathematical symbols which are rendered and displayed properly. This activity interacts with OliveWordTrialDemoActivity which is as follows:

public class OliveWordTrailDemoAcitivy extends Activity implements
OnClickListener, CommentListener, NoteListener, HyperlinkListener, ProgressListener {

    OliveWordOperator viu;

    EditText searchEditText;
    ArrayList<String> bookmarks;
    Handler handler;

    protected void onCreate(Bundle savedInstanceState) {
        viu = new OliveWordOperator(this, this);
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setProgressBarVisibility(true);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        setContentView(R.layout.demo_view);
        OliveWordView view = (OliveWordView) findViewById(R.id.test_view);

        try {
            viu.init(view, getIntent().getData());
            viu.start(viu.isEncrypted(), "111");
        } catch (Exception e) {
            e.printStackTrace();
        }
        handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                setProgress(msg.what * 10);
                super.handleMessage(msg);
            }

        };

    }

    @Override
    protected void onDestroy() {
        viu.release();
        super.onDestroy();
    }

    @Override
    public void getComment(ArrayList<String[]> comments) {
        for (int i = 0; i < comments.size(); i++) {
            AlertDialog.Builder builder = new Builder(this);
            builder.setTitle(comments.get(i)[0]).setMessage(comments.get(i)[1])
            .show();
        }
    }

    @Override
    public void getHyperlink(String hyperlink) {
        if (Uri.parse(hyperlink).getScheme().contains("mailto")) {
            try {
                startActivity(new Intent(Intent.ACTION_SENDTO,
                        Uri.parse(hyperlink)));
            } catch (ActivityNotFoundException anfe) {
                Toast.makeText(this, "can't found email application",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(hyperlink)));
        }
    }

    @Override
    public void getNote(SparseArray<String> notes) {
        for (int i = 0; i < notes.size(); i++) {
            AlertDialog.Builder builder = new Builder(this);
            if (notes.keyAt(i) == NoteListener.FOOTNOTE) {
                builder.setTitle("footnote").setMessage(notes.valueAt(i))
                .show();
            } else if (notes.keyAt(i) == NoteListener.ENDNOTE) {
                builder.setTitle("endnote").setMessage(notes.valueAt(i)).show();
            }
        }

    }

    public void goToBookmarks(String name) {
        viu.goToBookmark(name);
    }

    public void listBookmarks() {
        this.bookmarks = viu.listBookmarks();
    }

    @Override
    public void notifyProgress(int progress) {
        handler.sendEmptyMessage(progress);
    }

    @Override
    public void onClick(View v) {

    }

}

lib_trial 文件夹,你会发现它可以添加到您的libs文件夹,如果你想单独使用它的库。

In the lib_trial folder you will find the library which can be added to your libs folder if you want to use it separately.

和在 API 文件夹,你会发现该库的详细描述及其在PDF文件中,这是非常容易理解的形式方法。所以人们可以只直接使用这个库,并使用提供给他们的特定需求的方法。

And in the API folder you will find a detailed description of the library and its methods in form of a pdf file which is very easy to understand. so people can just use this library directly and use the methods provided to their specific requirement.

所以这是我的解决方案将与现在。没有更好的办法,欢迎选购。赏金时间即将获得很快结束,所以请提供您可以尽快有任何其他的解决办法。谢谢你。

So that's the solution am going with for now. Any better solutions are welcome. The bounty time is about to get over soon so please provide any other solution you may have as soon as possible. Thanks.

这篇关于显示一个微软Word文件的视图(TextView的说)在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆