-1

单击android中的按钮后如何显示文档文件,如何通过在Android中单击打开该文档?

是否可以在不使用应用程序的情况下显示文档。

4

2 回答 2

1

这将帮助您将 sdcard/dir 中的所有 .doc 文件添加到 arraylist 之后,只需将此数据填充到 ListView 或您想要的任何位置

        ArrayList<String> wordFiles = new ArrayList<String>();
        String currentFile;
        String path = "dir//"; 
        File sd = Environment.getExternalStorageDirectory();
        File folder = new File(sd,path);
        File[] listOfFiles = folder.listFiles(); 
        for (int i = 0; listOfFiles!=null &&i< listOfFiles.length; i++) 
        {

            if (listOfFiles[i].isFile()) 
            {
                currentFile = listOfFiles[i].getName();
                if (currentFile .endsWith(".doc"))
                {   
                    //currentFile =currentFile.replace(".doc", "");
                    wordFiles .add(currentFile);

                }
            }
        }

并打开 .doc (用户必须有一个可以处理 .docs 文件的应用程序)试试这个:

    File sd = Environment.getExternalStorageDirectory();
    File file = new File(sd, "dir//file.doc");
    Intent intent = new Intent();
    String type = "application/msword";
    intent.setDataAndType(Uri.fromFile(file), type);
    startActivity(intent); 
于 2012-10-08T16:44:21.913 回答
0

在这里,我尝试了递归文件列表,它列出了 sdcard 和 sdcard 的内部文件夹中的所有单词文件。可能它会为你工作。

package com.fileex;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class WordListActivity extends ListActivity {
    ArrayAdapter<String> adapter;
    int clickCounter = 0;
    ArrayList<String> listItems = new ArrayList<String>();
    ArrayList<String> listItems2 = new ArrayList<String>();
    private File[] wordfile;
    String[] wordlist, wordlist2;
    Button back, home;
    EditText edtsearch;
    ListView lv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doculist);

        edtsearch = (EditText) findViewById(R.id.edt_searchdoc);
        back = (Button) findViewById(R.id.btnbackdocu);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();

            }
        });
        home = (Button) findViewById(R.id.btnhomedocu);
        home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(WordListActivity.this, Home.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

            }
        });

        File sdcard = Environment.getExternalStorageDirectory();

        wordfile = sdcard.listFiles();

        for (int i = 0; i < wordfile.length; i++) {

            if (wordfile[i].isDirectory()) {
                walkdir(wordfile[i]);
            } else {
                if (wordfile[i].getAbsolutePath().endsWith(".doc")) {

                    listItems.add(wordfile[i].getName());
                    listItems2.add(wordfile[i].getAbsolutePath());

                }
            }
        }

        wordlist = new String[listItems.size()];
        wordlist = listItems.toArray(wordlist);
        wordlist2 = new String[listItems2.size()];
        wordlist2 = listItems2.toArray(wordlist2);

        adapter = new ArrayAdapter<String>(this, R.layout.listitemsimple,
                R.id.txtsimpleitem, wordlist);
        lv = getListView();
        registerForContextMenu(lv);

        lv.setAdapter(adapter);

        edtsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {

                String text = edtsearch.getText().toString()
                        .toLowerCase(Locale.getDefault());
                WordListActivity.this.adapter.getFilter().filter(text);

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }

        });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(WordListActivity.this,
                        "Filepath::::" + wordlist2[arg2], 5000).show();
                File stringtofile = new File(wordlist2[arg2]);
                PackageManager packageManager = getPackageManager();
                Intent testIntent = new Intent(Intent.ACTION_VIEW);
                testIntent.setType("application/msword");
                List list = packageManager.queryIntentActivities(testIntent,
                        PackageManager.MATCH_DEFAULT_ONLY);
                if (list.size() > 0 && stringtofile.isFile()) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    Uri uri = Uri.fromFile(stringtofile.getAbsoluteFile());
                    intent.setDataAndType(uri, "application/msword");
                    startActivity(intent);

                }
            }
        });

    }

    public void walkdir(File dir) {
        String pdfPattern = ".doc";

        File listFile[] = dir.listFiles();

        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {

                if (listFile[i].isDirectory()) {
                    walkdir(listFile[i]);
                } else {
                    if (listFile[i].getAbsolutePath().endsWith(pdfPattern)) {
                        // Do what ever u want
                        listItems.add(listFile[i].getName());
                        listItems2.add(listFile[i].getAbsolutePath());
                        System.out.println(listFile[i]);

                    }
                }
            }
        }
    }

}
于 2013-06-20T08:54:33.727 回答