본문 바로가기
TechNical/Android

SD카드 파일 리스트 읽어 오기

by 강멍멍이 2012. 5. 8.
반응형

package com.kei.fileexplorer;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class FileExplorer extends Activity {
 
 String mSdPath ="";
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String result = new String("");     
        TextView tv = (TextView)findViewById(R.id.text1);
       
        String ext = Environment.getExternalStorageState();

        if(ext.equals(Environment.MEDIA_MOUNTED)) {
         mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
         mSdPath = Environment.MEDIA_UNMOUNTED;
        }
       
        try{    
           File f = new File(mSdPath);
          
           String[] filenames = f.list(null);
          
           for (int i = 0; i < filenames.length ; i++){
            String filename = filenames[i];
            if (filename.toLowerCase().endsWith("jpg")) {
             result = result + filename + "\n";
            }
           }  
           
           tv.setText(result);
           
           }catch(Exception e){  
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();  
           }
    }
   
}

반응형

댓글