大家好 经过深思熟虑并查看了很多选项和解决方法,我认为OliveDocLibrary是最好的方法。这是一个链接,它将让您直接进入三个适用于 Android 的库的下载页面,它们分别是 DOC、XLS 和 PPT。所有这些工作都非常好。您将下载的包文件夹内将包含三个文件夹。哪个是:
- API
- lib_trial
- 演示
在演示文件夹中,您将找到 Word 的示例项目。您可以直接将此项目导入 Eclipse 中的工作区并自己测试代码。为方便起见,我在此处发布该代码。我删除了一些我认为没有必要的代码部分(在这里回答我的问题)。所以代码有两个文件,主要活动FileChooser
如下:
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();
}
}
}
在这里,我放置了一个名为simple.docx的 doc 文件,其中包括正确渲染和显示的图像和数学符号。该活动与之交互OliveWordTrialDemoActivity
如下:
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 文件夹中的库,如果您想单独使用它。
在API文件夹中,您会以非常容易理解的 pdf 文件的形式找到该库及其方法的详细说明。所以人们可以直接使用这个库,并使用提供给他们特定需求的方法。
所以这就是现在的解决方案。欢迎任何更好的解决方案。赏金时间即将结束,因此请尽快提供您可能拥有的任何其他解决方案。谢谢。