美文网首页
Android 常用工具类之 DatabaseExportUti

Android 常用工具类之 DatabaseExportUti

作者: Kevin_小飞象 | 来源:发表于2021-05-08 10:55 被阅读0次

应用数据库导出工具类,此工具类不是网上大家用烂的那一份,是博主亲自编写,亲自测试,代码简洁清晰,可满足日常开发。

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

/**
 * Created on 2021/4/13 14:21
 * 应用数据库导出工具类
 * @author Gong Youqiang
 */
public class DatabaseExportUtils {
    private static final boolean DEBUG = true;
    private static final String TAG = "DatabaseExportUtils";

    /**
     * Don't let anyone instantiate this class.
     */
    private DatabaseExportUtils() {
        throw new Error("Do not need instantiate!");
    }

    /**
     * 开始导出数据 此操作比较耗时,建议在线程中进行
     *
     * @param context      上下文
     * @param targetFile   目标文件
     * @param databaseName 要拷贝的数据库文件名
     * @return 是否倒出成功
     */
    public boolean startExportDatabase(Context context, String targetFile,
                                       String databaseName) {
        if (DEBUG) {
        }
        if (!Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            if (DEBUG) {
            }
            return false;
        }
        String sourceFilePath = Environment.getDataDirectory() + "/data/"
                + context.getPackageName() + "/databases/" + databaseName;
        String destFilePath = Environment.getExternalStorageDirectory()
                + (TextUtils.isEmpty(targetFile) ? (context.getPackageName() + ".db")
                : targetFile);
        boolean isCopySuccess = FileUtils
                .copyFile(sourceFilePath, destFilePath);
        if (DEBUG) {
            if (isCopySuccess) {

            } else {
            }
        }
        return isCopySuccess;
    }
}

相关文章

网友评论

      本文标题:Android 常用工具类之 DatabaseExportUti

      本文链接:https://www.haomeiwen.com/subject/nwxclltx.html