Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Java
- 메서드
- JavaScript
- mybatis
- 오버로딩
- 기본
- 국제화
- 안드로이드
- Menu
- HTML
- 어노테이션
- 에러페이지
- struts2
- Eclips
- JSP
- Android
- 메소드
- AWT
- 생성자
- 예외처리
- OGNL
- Spring
- Graphic
- 클래스
- paint
- oracle
- 이클립스
- layout
- 전화걸기
- 배열
Archives
- Today
- Total
note
SD CARD (SD 카드에 저장하기) 본문
SD Card 값 안줬으면은 에뮬끄고 수정누른뒤 512정도로 용량 확보하고 시작합니다
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/output" /> </LinearLayout>
package kr.android.sdcard;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;
public class SDCardDemo extends Activity {
// 파일의 이름 생성
String fileName = "test-" + System.currentTimeMillis() + ".txt";
private TextView readOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.readOutput = (TextView) this.findViewById(R.id.output);
// 파일 생성
wirteFileToSDcard();
// 파일 읽기
readFileFromSDcard();
}
private void wirteFileToSDcard() {
readOutput.setText("[파일쓰기]\n");
// /sdcard 디렉토리에 대한 참조 설정
File sdDir = new File("/sdcard");
if (sdDir.exists() && sdDir.canWrite()) {
// 경로 설정
File uadDir = new File(sdDir.getAbsolutePath() + "/filetest");
uadDir.mkdir(); // mkdir()를 사용하여 디렉토리 생성
FileOutputStream fos = null;
try {
fos = new FileOutputStream(uadDir.getAbsolutePath() + "/"
+ fileName);
String msg = "즐거운 하루!!"; // 파일에 기술될 내용
fos.write(msg.getBytes());
readOutput.append("파일이 생성되었습니다.\n");
} catch (FileNotFoundException e) {
Toast.makeText(this, "생성된 파일이 없습니다,", 4000).show();
} catch (IOException e) {
Toast.makeText(this, "파일에 내용을 기록중 오류 발생", 4000).show();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private void readFileFromSDcard() {
readOutput.append("[파일 읽기]\n");
File rFile = new File(Environment.getExternalStorageDirectory()
+ "/filetest/" + fileName);
//존재하고 읽을 수 있으면 실행
if (rFile.exists() && rFile.canRead()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(rFile);
byte[] reader = new byte[fis.available()];
fis.read(reader);
readOutput.append(new String(reader));
} catch (IOException e) {
Toast.makeText(this, "파일을 읽을 수 없습니다", 4000).show();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
'Android > 기본' 카테고리의 다른 글
이클립스 구글맵 api 설치하기 및 자신의 위치 알아내기(경도,위도) (0) | 2012.01.17 |
---|---|
SQLite (0) | 2012.01.13 |
Access Web Image (0) | 2012.01.13 |
Read JSON (0) | 2012.01.13 |
XML Resource (0) | 2012.01.13 |