|
안드로이드(Android) 간단한 파일 브라우저 만들기 |
|
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
|
디자인이 전혀 없는 파일브라우저를 하나 만듭니다. 제일 상위 루트부터 시작해서 클릭시 세부 폴더로 들어갈수 있으며 현재 위치에서 상위 폴더로 이동이 가능합니다. 그리고 파일을 클릭했을 때 이벤트를 잡아 파일명을 담은 메시지 박스를 띄우게 됩니다. |
파일 브라우저를 구현하기 위해 스마트폰에
있는 파일리스트를 가져와야 합니다.
아래 처럼 path 주소만 넣으면 전체
파일 주소를 리스트로 가져오게 됩니다.
File f = new File(dirPath); File[] files = f.listFiles();
그리고 가져온 파일 리스트를 ArrayList<String> 객체에
담게 되는데 현재 리스트는 item 리스트에 담고
하위 폴더나 파일명은 path 리스트에 담습니다.
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());
}
이렇게 만든 현재 파일리스트, item 객체를
ArrayAdapter 의 인수로 넘기고 레이아웃을
R.layout.row 로 넣게 됩니다. R.layout.row은
단위 행에 해당하는 xml 레이아웃 입니다.
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item); setListAdapter(fileList);
R.layout.row 레이아웃 입니다. 단위행에
해당하는 TextView 인데 파일 주소를 나타내기
위한 행높이와 글자 크기를 셋팅했네요
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="33dp"
android:textSize="23sp" />
이번에는 행을 선택했을 때 상위로 가던지 아니면
하위 폴더로 갈수 있도록 클릭 이벤트를
잡아 줍니다. File 객체를 통해 현재 값이
디렉토리인지, read 가 가능한지 판단해서
getDir() 함수를 실행해 파일 목록을 표현 합니다.
만약 폴더를 읽지 못한다면 읽지 못한다는
메시지 박스를 띄워 줍니다.
if (file.canRead())
getDir(path.get(position));
else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.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
}
}).show();
}
메인 activity 의 전체 레이아웃 xml 입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data" />
</LinearLayout>
다음은 메인 activity 의 전체 소스 입니다.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class SampleActivity25 extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root = "/";
private TextView mPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_activity25);
mPath = (TextView) findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath) {
mPath.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.ic_launcher)
.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
}
}).show();
}
} else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 이미지뷰에서 Exif 정보 다이얼로그 띄우기 (1) | 2014.11.15 |
|---|---|
| 안드로이드(Android) showDialog(int) deprecated 다시 구현하기 (0) | 2014.11.14 |
| 안드로이드(Android) 파일 브라우저에서 이미지 보여주기 (1) | 2014.11.13 |
| 안드로이드(Android) 파일브라우저에서 이미지 클릭시 Exif 정보 출력하기 (0) | 2014.11.12 |
| 안드로이드(Android) 지니모션(GenyMotion) 에뮬레이터 파일 옮기기 (0) | 2014.11.10 |
| 안드로이드(Android) 사진의 EXIF 정보 가져오기 (1) | 2014.11.09 |
| 안드로이드(Android) XML 을 이용해 옵션메뉴를 구성하는 방법 (3) | 2014.11.09 |
| 안드로이드(Android) 위치값을 입력하여 구글 지도 이동하기 (5) | 2014.11.08 |

