안드로이드 개발 질문/답변
(글 수 45,052)
public class BufferedWriterFactory {
public static
BufferedWriter create(String path, String TextFileName) throws IOException
{
File filePath = new
File(path);
if
(!filePath.exists())
filePath.mkdirs();
BufferedWriter bw = new
BufferedWriter(new FileWriter(path + File.separator +
TextFileName));
return
bw;
}
}
class ButtonSaveClickListener implements OnClickListener {
Context
context;
public
ButtonSaveClickListener(Context context)
{
this.context = context;
}
public void onClick(View
v){
mChrono.stop();
String TextFileName =
""+mYear+""+mMonth+""+mDay+".txt" ;
String TextContent =
mChrono.getText().toString();
BufferedWriter bw = null;
try{
bw =
BufferedWriterFactory.create("/sdcard/RespirationCheck",TextFileName);
bw.write(TextContent);
Toast.makeText(this.context, "성공적으로 기록하였습니다.",
Toast.LENGTH_SHORT).show();
}
catch(IOException
e){
String exceptionMessage = TextFileName + " 파일에 기록할 수
없습니다.";
Toast.makeText(this.context, exceptionMessage,
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch
(Exception e)
{
Toast.makeText(this.context, "알 수 없는 오류입니다.",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try
{
if (null !=
bw)
bw.close();
}
catch (IOException
e)
{
Toast.makeText(this.context, TextFileName + "파일을 닫을 수 없습니다.",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
메니페스트에 퍼미션도 주고 다음과 같이 코드도 작성하였는데 계속 파일에 기록할수 없습니다만 뜨네요 ㅜㅠ 왜이럴까요
12-19 15:58:19.152: E/Trace(964): error opening trace file: No such file or directory (2)
이건 로그캣 빨간 부분입니다.
처음올리는 질문입니다. 공지사항읽었습니다.
2012.12.20 13:36:46
public class BufferedWriterFactory {
private static
final String TAG = null;
public static BufferedWriter create(String path, String TextFileName)
throws IOException {
// 경로(path)가
존재하는지 확인하고, 그것이 없으면 그것을 만듭니다.
File filePath = new
File(path);
if
(!filePath.exists())
filePath.mkdirs();
String
sdcard;
String state =
Environment.getExternalStorageState();
Log.i(TAG, "storage
state:"+state);
if(state.equals(Environment.MEDIA_MOUNTED)){
sdcard =Environment.getExternalStorageDirectory().getAbsolutePath(); // sd카드경로
}
else{
sdcard =
Environment.MEDIA_UNMOUNTED;
Log.i(TAG, "media unmounted");
}
File dir = new
File(sdcard+"/Measure");
dir.mkdir();
File file = new
File(sdcard+"/Measure/"+TextFileName);
BufferedWriter bw = new
BufferedWriter(new FileWriter(path + File.separator +
TextFileName));
return
bw;
}
}
우선 댓글 달아주셔서 감사합니다.
근데 디렉토리를 다음과 같이 추가해도 역시 안되네요 ㅜㅠ 왜그런걸 까요..ㅜㅠㅜㅠ\
12-20 04:23:30.151: E/Trace(1219): error opening trace file: No such file or directory (2)
12-20 04:26:45.380: E/Trace(1314): error opening trace file: No such file or directory (2)
12-20 04:27:27.569: E/Trace(1361): error opening trace file: No such file or directory (2)
여전히 다음과 같은 로그캣에러가 떠요 ㅜㅠ




디렉토리는 만들어졌나요?
String sdcard;
String state = Environment.getExternalStorageState();
Log.i(TAG, "storage state:"+state);
if(state.equals(Environment.MEDIA_MOUNTED)){
sdcard =Environment.getExternalStorageDirectory().getAbsolutePath(); // sd카드경로
}
else{
sdcard = Environment.MEDIA_UNMOUNTED;
Log.i(TAG, "media unmounted");
}
File dir = new File(sdcard+"/폴더명");
dir.mkdir();
File file = new File(sdcard+"/폴더명/"+파일명);
이렇게 함 해보세요~