2011-01-08 37 views
3

我只想在模擬器上顯示sdcard中的文件內容(如圖像文件/視頻文件/音樂文件那樣)。從android sdcard中讀取數據

以下是我的代碼。

public class listfiles extends ListActivity { 
private ArrayList<String> item = null; 
private ArrayList<String> path = null; 
private String root="/"; 
private TextView myPath; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sub); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir(root); 
    } 

    private void getDir(String dirPath) 
    { 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 
     path.add(file.getPath()); 
     if(file.isDirectory()) 
     item.add(file.getName() + "/"); 
     else 
     item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "]") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     } 
     }).show(); 
    } 
} 
} 

在我的輸出中我得到了文件路徑&文件名。但是當我點擊文件時,它不會顯示內容。我該怎麼做?由於

最後我得到it.My糾正代碼如下所示..

public class SDCardActivity extends ListActivity { 
private List<String> item = null; 
private List<String> path = null; 
private String root="/sdcard"; 
private TextView myPath; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Intent intent=getIntent(); 

     setContentView(R.layout.sub); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir(root); 
    } 

    private void getDir(String dirPath) 
    { 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 
     path.add(file.getPath()); 
     if(file.isDirectory()) 
     item.add(file.getName() + "/"); 
     else 
     item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which){ 
     // TODO Auto-generated method stub 
      dialog.dismiss(); 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri uri = Uri.parse("file://" + file.getPath()); 
     String fname=file.getName(); 
     if(fname.endsWith(".jpeg")||fname.endsWith("png")||fname.endsWith(".gif")) 
     { 
      intent.setDataAndType(uri, "image/*"); 
      startActivity(intent); 
     } 
     else if(fname.endsWith(".mp4")||fname.endsWith(".3gp")) 
     { 
      intent.setDataAndType(uri, "video/*"); 
      startActivity(intent); 
     } 
     else if(fname.endsWith(".mp3")) 
     { 
      intent.setDataAndType(uri, "audio/*"); 
      startActivity(intent); 
     } 
     else 
      try { 
       EditText tv = (EditText)findViewById(R.id.tn); 
       StringBuilder text = new StringBuilder(); 

       BufferedReader br = new BufferedReader(new FileReader(file)); 
       String line; 

       while ((line = br.readLine()) != null) { 
        text.append(line); 
        text.append('\n'); 

        //Set the text 
        tv.setText(text); 

       } 
      }//try 
      catch (IOException e) { 
       //You'll need to add proper error handling here 
      }//catch 

    } 
} 
} 

回答

4

也許我已經錯過了它在你的代碼,但我不能發現其中的任何Intent。您必須撥打Intent以及ACTION_VIEW標誌作爲您想要顯示的任何文件。

例如,

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW); 
Uri imgUri = Uri.parse("file://" + file.getPath()); 
intent.setDataAndType(imgUri, "image/*"); 
startActivity(intent); 

你簡單的創建一個Intent例如,設置這是在我們的例子ACTION_VIEW的動作。然後,通過將文件對象的路徑連接到file://來創建一個Uri對象。你現在要做的就是通過指定uri和一個字符串來設置數據和類型。在我的例子中,每個圖像類型。但是你可以指定一個特定的圖像類型。一旦你的意圖被設置並準備就緒,你就可以通過以意圖爲參數啓動一個活動來啓動它。

Android將負責尋找合適的應用程序以在意圖中顯示數據。

+0

謝謝你的答覆。但是我要把你的代碼插入我的。 – sanjay

+0

@Sudha onListItemClick – 2011-01-08 18:25:05

+0

非常感謝,我終於明白了。 – sanjay

8

下面的代碼展示瞭如何從SDcard.simple中讀取文件內容,在SD卡中插入一個文本文件並在程序中實現以下代碼。

try{ 
      File f = new File(Environment.getExternalStorageDirectory()+"/f1.txt"); 
      fileIS = new FileInputStream(f); 
      BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); 
      String readString = new String(); 
      //just reading each line and pass it on the debugger 
      while((readString = buf.readLine())!= null){ 
       textdata.setText(readString); 
       Log.d("line: ", readString); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     }