Tuesday, April 14, 2015

Android 파일 탐색기 예제

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 리스트뷰 및 패스 얻기
ListView listView1 = (ListView) findViewById(R.id.listView1);
String path = Environment.getExternalStorageDirectory().getAbsolutePath();

// 폴더 리스트 읽기
File file = new File(path);
File[] files = file.listFiles();

ArrayList<String> fileList = new ArrayList<String>();

for (int i = 0; i < files.length; i++) {
File subFile = files[i];
//String absPath = subFile.getAbsolutePath();
String absPath = subFile.getName();
fileList.add(absPath);
}

ArrayAdapter<String> fileAdapter = new ArrayAdapter<String>(this, R.layout.item, R.id.listItem1, fileList);
listView1.setAdapter(fileAdapter);
}
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.explorer.MainActivity" >

<ListView android:id="@+id/listView1"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   
</ListView>
</RelativeLayout>

item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
<TextView
   android:id="@+id/listItem1"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:textSize="20dp">
       
</TextView>
</RelativeLayout>


No comments:

Post a Comment