http://babtingdev.tistory.com/261
파일의 위치는 /data/data/패키지명/files 디렉토리로 정해져 있으며 임의 경로의 파일을 마음대로 열 수 없다.
http://www.androidpub.com/index.php?mid=android_dev_qna&page=6&document_srl=2148245
byte str_data[] = null;
try {
InputStream is = getAssets().open(filename);
int size = is.available();
str_data = new byte[size];
is.read(str_data);
is.close();
}
catch(Exception e){ e.printStackTrace(); }
이건 asset에 파일 넣고 읽는 것인데요.. txt파일 읽은 것인데.. 하여간
asset 폴더에 a.dat 이런식으로 넣었으면 filename에 "a.dat" 넣으시면 됩니다.
안드로이드 assets 에 관하여 (assets 폴더에 있는 파일 전체 sdcard에 복사)
//퍼미션 중요
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
String mkdir = null ;
try {
files = assetManager.list("");
//이미지만 가져올때 files = assetManager.list("image");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(files[i]);
//폴더생성
String str = Environment.getExternalStorageState();
if ( str.equals(Environment.MEDIA_MOUNTED)) {
mkdir = "/sdcard/elecgal/templet/" ;
} else {
Environment.getExternalStorageDirectory();
}
File mpath = new File(mkdir);
if(! mpath.isDirectory()) {
mpath.mkdirs();
}
//
out = new FileOutputStream("/sdcard/elecgal/templet/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
//퍼미션 중요
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
String mkdir = null ;
try {
files = assetManager.list("");
//이미지만 가져올때 files = assetManager.list("image");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(files[i]);
//폴더생성
String str = Environment.getExternalStorageState();
if ( str.equals(Environment.MEDIA_MOUNTED)) {
mkdir = "/sdcard/elecgal/templet/" ;
} else {
Environment.getExternalStorageDirectory();
}
File mpath = new File(mkdir);
if(! mpath.isDirectory()) {
mpath.mkdirs();
}
//
out = new FileOutputStream("/sdcard/elecgal/templet/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
http://stackoverflow.com/questions/9967904/aasset-read-vs-fopen
'Programming Android' 카테고리의 다른 글
How to use Multi-touch in Android 2: Part 3, Understanding touch events (0) | 2012.07.04 |
---|---|
안드로이드 Failed to install on device timeout (0) | 2012.07.03 |
(Android) NDK tips (0) | 2012.06.30 |
Android Game Development (0) | 2012.06.27 |
Log - Android Developers (0) | 2012.06.27 |