안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요.
1. AVD Manager 에서 virtual devices를 만들고
2. dat 파일을 res/raw/test.dat 에 복사하고
try {
Resources myResources = getResources();
InputStream myFile = myResources.openRawResource(R.raw.secfile);
DataInputStream dataIO = new DataInputStream(myFile);
...
readarry[0] = dataIO.readByte();
이렇게 하고 있는데
test.dat 파일용량이 1024k 까지는 제대로 읽어오는데
1024k 를 넘어서면 읽어오지 못합니다.
그걸 보면 1MByte가 넘고 안넘고가 무슨 상관이 있는것 같습니다.
virtual device를 이용하면 제약이 있는건가요?
소스는 아래와 같습니다.
package test.example.com;
import java.io.DataInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;
import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.widget.TextView;
public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byte[] read4byte = new byte[4];
int skip = 0;
int strAddr = 0;
StringBuffer strBuffer = new StringBuffer();
String strLine = null;
try {
Resources yourResources = getResources();
InputStream secFile = yourResources.openRawResource(R.raw.last);
DataInputStream secIO = new DataInputStream(secFile);
for(int nth=0; nth<4; nth++)
read4byte[nth]= secIO.readByte();
strAddr = getReadAddr(read4byte);
skip = secIO.skipBytes(strAddr - 4);
strLine = secIO.readLine();
strBuffer.append(strLine + "\n");
secIO.close();
secFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
TextView ts = (TextView) findViewById(R.id.secstr);
ts.setText(strBuffer);
}
private int getReadAddr(byte [] ibuf) {
int section;
section = ibuf[3] << 24;
section |= ibuf[2] << 16;
section |= ibuf[1] << 8;
section |= ibuf[0];
return section;
}
}res/raw/last.dat 파일을 등록했습니다.
dat 파일은 index + text 파일로 구성되었고 index 에서 원하는 스트링이 시작하는 주소를 읽어와서
그곳으로 이동후 스트링을 readline 하고 화면에 뿌리는 동작입니다.
감사합니다.




제가 리소스에서 파일로 전환할때 쓰고있는 함수 붙여드립니다.
void setRes(int resID,String nm){
try{
nm = "/sdcard/"+nm;
if(new java.io.File(nm).isFile());
else{
byte[] w = new byte[1024];
java.io.InputStream in = getResources().openRawResource(resID);
java.io.FileOutputStream out = new java.io.FileOutputStream(nm);;
int len = 0;
while((len = in.read(w))>0)
out.write(w,0,len);
in.close();
out.close();
}
}catch(Exception e){}
}