2012-10-08 118 views
-1

如何在單擊Android中的按鈕後顯示doc文件,以及如何通過單擊Android打開該文檔的該文檔?如何在Android中顯示從SD卡的DOC文件列表?

是否可以在不使用應用程序的情況下顯示文檔。

+1

如果你想自己顯示一個.doc文件(沒有意圖,沒有外部應用程序),你必須自己編寫一個.doc解析器。 – m0skit0

回答

1

這將幫助你的SD卡/目錄內的所有.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

'for(File current:listOfFiles)'順便說一句,如果你想打開這些文件,你將無法獲得完整的路徑。最好返回一個File對象列表。 – m0skit0

0

在這裏,我曾與遞歸文件列表嘗試,從SD卡和SD卡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]); 

        } 
       } 
      } 
     } 
    } 

} 
相關問題