티스토리 뷰

모바일 프로그래밍

Android 파일 탐색기 예제

두덕리온라인 2016. 4. 7. 09:31
728x90
반응형

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>


반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday