sd카드에서 텍스트파일을 열 수 있는 파일을 만들고 있습니다. 근데 파일여는 부분이 도저히 안되서요 ㅠㅠ
제가 프로그래밍을 잘하는것도 아니고 안드로이드는 처음이라 어렵네요 ㅠㅠ 실행은 되는데, 텍스트 파일을 여는 부분만 안됩니다 ㅠㅠ 아신다면 답변부탁드려요.밑에 파일은 인터넷에서 찾아서 제가 약간 추가한 소스입니다.
public class MainActivity extends ListActivity {
/* item은 탐색기에 표시될 내용, path는 item 클릭시 이동할 경로이다. */
private List<String> item = null;
private List<String> path = null;
/* 루트 디렉토리 설정 */
private String root="/sdcard/";
/* 현재 경로를 저장해주는 변수 myPath */
private TextView myPath;
BufferedReader in;
InputStream myFile;
Resources myResources;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.filemain);
/* myPath 뷰어 설정 */
myPath = (TextView)findViewById(id.path);
getDir(root);
}
--------이 사이에 소스들은 용량초과로 생략합니다 ㅠㅠ
/* 만약 디렉토리가 아닌 파일을 클릭한 거라면 */
else{
int index = (file.getName()).indexOf(".txt");
//파일 이름에 txt가 없다면 -1을 리턴
if(index != -1){ //텍스트 파일인 경우만 실행
String name = file.getName();
FileInputStream fis;
char[] data = null;
try{
fis = openFileInput(name);
Reader in = new InputStreamReader(fis,"UTF8");
int n = fis.available();
Log.i("MIN", String.valueOf(n));
data = new char[n];
while(in.read(data)!=-1){;}
fis.close();
}catch(Exception e){;}
}
else{ //텍스트 파일이 아닌경우 다이얼로그를 이용하여서 파일 이름만 뜬다.
new AlertDialog.Builder(this)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
private InputStream openFileInput(File file) {
// TODO Auto-generated method stub
return null;
}
}
=====================xml파일
filemain.xml
<?xml version="1.0" encoding="utf-8"?>
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>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 탐색기 리스트에 나타나는 각각의 아이템 -->
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="100px"
android:textSize="23sp"
/>