首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何模拟getApplicationInfo().dataDir

如何模拟getApplicationInfo().dataDir
EN

Stack Overflow用户
提问于 2016-05-03 18:41:22
回答 1查看 1.2K关注 0票数 0

我们如何模拟getApplicationInfo.dataDir?以下是代码代码片段:

代码语言:javascript
复制
 private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;
        DATABASE_PATH = mContext.getApplicationInfo().dataDir + "/databases/";
        try {
            createDataBase();
            openDataBase();
        } catch (IOException e) {
            throw new Error("ErrorCopyingDataBase");
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2016-05-03 20:17:48

你可以看看我的代码,我以前是怎么做的:

代码语言:javascript
复制
public class DataBaseConfiguration extends SQLiteOpenHelper {
public static String DB_PATH;
public static String DB_NAME;

public static SQLiteDatabase _database;
private final Context myContext;

/**
 * @param builder
 * @author Wild Coder
 * @description
 * @daJun 24, 2015
 */
public DataBaseConfiguration(Builder builder) {
    super(builder.CONTEXT, builder.DB_NAME, null, builder.VERSION);
    this.myContext = builder.CONTEXT;
    DB_PATH = builder.DB_PATH;
    DB_NAME = builder.DB_NAME;
}

public static class Builder {
    public String DB_PATH;
    public String DB_NAME;
    public Context CONTEXT;
    public int VERSION;

    /**
     * @param context Context
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder(Context context) {
        this.CONTEXT = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/";
    }

    /**
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder setName(String name) {
        DB_NAME = name;
        return this;
    }

    /**
     * @description Set DBConfiguration
     * @author Wild Coder
     */
    public Builder setName(int version) {
        VERSION = version;
        return this;
    }

    /**
     * Build the configuration for storage tool.
     *
     * @return DBConfiguration
     * @author Wild Coder
     */
    public DataBaseConfiguration build() {
        return new DataBaseConfiguration(this);
    }
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 */
public void createDataBase() {
    boolean dbExist = checkDataBase();
    if (!dbExist) {
        // 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.
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @void
 * @use Wild Coder
 */
public void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.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[2048];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @SQLiteDatabase
 * @use Wild Coder
 */
public static SQLiteDatabase openDataBase() throws SQLException {
    // Open the database
    if (_database == null) {
        _database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
                SQLiteDatabase.OPEN_READWRITE
                        | SQLiteDatabase.CREATE_IF_NECESSARY);
    } else if (!_database.isOpen()) {
        _database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
                SQLiteDatabase.OPEN_READWRITE
                        | SQLiteDatabase.CREATE_IF_NECESSARY);
    }
    return _database;
}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @void
 * @use Wild Coder
 */
public static void closeDatabase() {
    if (_database != null && _database.isOpen())
        _database.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

/**
 * @author Wild Coder
 * @description
 * @dJun 24, 2015
 * @boolean
 * @use Wild Coder
 */
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    // The directory is now empty so delete it return dir.delete(); }
    return dir.delete();
}

}

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37001942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档