如何在Android中显示sdcard的doc文件列表?

时间:2012-10-08 16:14:42

标签: java android

如何在点击Android中的按钮后显示doc文件,如何通过点击Android在Android中打开该视图?

是否可以在不使用app的情况下显示doc。

2 个答案:

答案 0 :(得分: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); 

答案 1 :(得分:0)

这里我试过了递归文件列表,列出了sdcard和sdcard的innerfolder中的所有word文件。可能会对你有用。

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]);

                    }
                }
            }
        }
    }

}