안드로이드 개발 질문/답변
(글 수 45,052)
프로젝트폴더 내의 assets 폴더에 .db 파일을 넣고 apk파일로 만든다음 배포했습니다.
그러면 사용자가 마켓에서 이 어플을 다운받으면 자동적으로 db파일도 포함되있는 것 아닌가요?
하지만 아예 db를 읽지 못하고(아예 없는건지 테이블 생성을 못시키는건지 이유를 모르겠습니다) 어플은 다운되버리네요..
웹 배포는 할 줄도 모르고 어짜피 db파일의 크기도 160kb밖에는 안되서 그냥 apk와 함께 포함시켜 배포하려는데 실제로 앱을 다운받으면 왜 db를 못 여는건지 궁금합니다..
다음은 db관련 소스입니다
import java.io.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.database.*;
import android.database.sqlite.*;
public class DBHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.songwol.train/databases/";
private static String DB_NAME = "latlng.db";
private SQLiteDatabase sqliteDB;
private final Context context;
private static String DB_PATH = "/data/data/com.songwol.train/databases/";
private static String DB_NAME = "latlng.db";
private SQLiteDatabase sqliteDB;
private final Context context;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
super(context, DB_NAME, null, 1);
this.context = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
//db파일이 존재하는지 열었다 닫으면서 확인만 한다.
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
sqliteDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
sqliteDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if(sqliteDB != null)
sqliteDB.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
if(sqliteDB != null)
sqliteDB.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}




코드 문제가 아닙니다..
apk만들때 미디어 파일 말고는 다 압축이 되어버려서 아마 배포를 못할겁니다
여기서는 방법이 2가지가 있습니다
1. 확장자를 mp3로 바꾸면 그냥 ..됩니다..-_-;;방법은 구글링!
2. 해당 디비파일을 복사시키는 겁니다...새로운 디비를 만드는거죠...이것도 방법은 구글링!!
저도 디비 배포해서 하는데...두번째 방법을 썻네요
용량이 900이 넘으면 안된다는건 최소메모리때문에 그런거였는데 2.3이후부터는 그것도 없어요~ 막 넣어도 된다능!