마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

텍스트뷰어 질문입니다.

0 추천
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"?>
<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>
 
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" 
    />
 
ksj (150 포인트) 님이 2013년 6월 17일 질문

1개의 답변

0 추천
 
채택된 답변

1. openFileInput 의 내용이 없네요

2. 데이터는 data = new char[n]; 에 담깁니다. data 를 Log.d 로 출력해보세요

3. 2의 과정에서 데이터가 담겨 있다면 해당 내용을 TextView 에 넣으면 됩니다.

aucd29 (218,390 포인트) 님이 2013년 6월 18일 답변
ksj님이 2013년 6월 18일 채택됨
감사합니다 ㅠㅠ Log.d(data,"msg"); 이런식으로 하면되나요?
근데 data가 char값이라서 String으로바꾸라는데 어떻게해야되져??
Log.d("tag", "msg " + new String(data));
감사합니다 ㅠㅠ 하나만 더 여쭤볼께요
제가 string name = file.getName();으로 해놓았는데요, 그러면 이름만 가져가니까 안되잖아요? openfileinput으로 열려는건데 openfileinput(file);로 하면 String값이 아니라서 안되더군요, 어떤걸 사용해야되나요 ㅠㅠ
어떤 질문인지 의미 파악이 안됩니다.
openfileinput(name);으로 소스가 되어있는데요, name이 file.getName();을 해서 파일의 이름만 가져오는거 아닌가요? 이게 맞는 소스인지 아닌지를 모르겠습니다..
해당 소스 코드가 위에 없으니 답변 드릴 수 없네요
...