작성 목적
- 앱 동작중에 파일을 읽어와야 하는 부분이 있어 Asset 내부의 파일을 복사하여 앱 내부저장소에 저장하도록 개발
간략 설명
- 앱 내부의 디렉터리에 CopyFileDir 디렉터리를 생성후, 내부에 conf, log 디렉터리 생성
- conf 디렉터리 내부에 asset에 있는 test_conf.ini 파일 저장
Sample Code
public class MainActivity extends AppCompatActivity {
String filePath = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Asset에 있는 파일을 내부저장소로 복사
filePath = "/data/data/" + MainActivity.this.getPackageName() + "/files/CopyFileDir/";
File file = new File(filePath);
if(!file.exists()) {
saveData(MainActivity.this);
}
}
public void saveData(Context context){
File confDir = new File(filePath + "conf/");
File logDir = new File(filePath + "log/");
try {
//ConfDir이 없을 경우 디렉터리 생성
if(!confDir.exists()) {
confDir.mkdirs(); //지정 경로을 찾아가며 없는 디렉터리는 자동으로 생성
logDir.mkdir(); //지정된 위치의 디렉터리만 생성
}
copyFile(context, "conf/test_conf.ini");
}catch (Exception e){
e.printStackTrace();
}
}
public void copyFile(Context context, String fileName){
try {
InputStream inputStream = context.getAssets().open(fileName);
OutputStream outputStream = new FileOutputStream(filePath + fileName);
byte[] buf = new byte[1024];
int length;
while((length = inputStream.read(buf)) > 0){
outputStream.write(buf, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
'Programming > Android' 카테고리의 다른 글
Android KeyStore 사용 방법 Sample (0) | 2022.11.27 |
---|---|
[React-Native] react-native-webview 에러 해결 (0) | 2022.09.22 |
Google Play 정책 위반 관련 참고사항 (0) | 2022.09.20 |
AAB(Android App Bundle) 생성 방법 (0) | 2022.05.10 |
JAVA 설치 및 환경변수 설정 (0) | 2020.02.11 |