安卓开发 assets下文件拷贝

版权声明:没有申明版权,你们随意! https://blog.csdn.net/baby986911/article/details/86685726

测试手机7.0

例如: assets文件下数据库名称为 qd.db

权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

需要动态申请。我的其他文章有,自己找找。

1、定义一个接口。

/**
 * createdUser : xuyanyun
 * createData :  2019/1/24/024
 * remark : 拷贝项目的接口
 * hasBug? 佛祖保佑!
 * name :
 */

public interface CopyDataBaseInterFace {
    void copySuccess();

    void copyFailed();
}

2、写一个创建文件的类。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import hdd.com.qdmanager.FrameApplication;

/**
 * createdUser : xuyanyun
 * createData :  2019/1/24/024
 * remark :
 * hasBug? 佛祖保佑!
 * name :
 */

public class CopyAssetsToFir {
    InputStream is ;
    FileOutputStream fos ;
    public boolean copyAssetsFile(String fileName, String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                 is = FrameApplication.getInstance().getAssets().open(fileName);
                 fos = new FileOutputStream(new File(path));
                byte[] buffer = new byte[4096];
                int byteCount = 0;
                // 循环从输入流读取buffer字节
                while ((byteCount = is.read(buffer)) != -1) {
                    // 将读取的输入流写入到输出流
                    fos.write(buffer, 0, byteCount);
                }
                fos.flush();// 刷新缓冲区
                return true ;
            } catch (Exception e) {
                e.printStackTrace();
                return false  ;
            }finally {
                try {
                    if (null != is){
                        is.close();
                        is = null ;
                    }
                    if (null != fos){
                        fos.close();
                        fos = null ;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else{
            return true ;
        }
    }
}

3、定义一个线程

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import java.io.File;

import hdd.com.qdmanager.FrameApplication;
import hdd.com.qdmanager.p_interface.CopyDataBaseInterFace;
import hdd.com.qdmanager.util.CopyAssetsToFir;

/**
 * createdUser : xuyanyun
 * createData :  2019/1/24/024
 * remark :
 * hasBug? 佛祖保佑!
 * name :
 */

public class CopyDataBaseRunnable implements Runnable {
    CopyAssetsToFir copyAssetsToFir;
    CopyDataBaseInterFace copyDataBaseInterFace ;
    //pad使用的数据库
    String copyPath = Environment.getExternalStorageDirectory() + File.separator + "qdCopy" + File.separator  ;
    //导出数据的数据库
    String exportPath = Environment.getExternalStorageDirectory() + File.separator + "qdExport" + File.separator ;
    String fileName = "qd.db";
    public CopyDataBaseRunnable(CopyDataBaseInterFace copyDataBaseInterFace ) {
        copyAssetsToFir = new CopyAssetsToFir() ;
        this.copyDataBaseInterFace = copyDataBaseInterFace ;
    }

    @Override
    public void run() {
        File file = new File(copyPath);
        File file2 = new File(exportPath);
        //判断文件夹是否创建
        if (!file.exists()) {
            file.mkdirs();
            systemToScan(copyPath);
        }
        if (!file2.exists()) {
            file2.mkdirs();
            systemToScan(exportPath);
        }
        boolean copyValue = copyAssetsToFir.copyAssetsFile(fileName ,copyPath + fileName) ;
        boolean exportValue = copyAssetsToFir.copyAssetsFile(fileName ,exportPath + fileName) ;
        if (copyValue && exportValue){
            copyDataBaseInterFace.copySuccess();
        }else{
            copyDataBaseInterFace.copyFailed();
        }
    }

    /**
     * 创建文件夹以后,防止在电脑中打开磁盘无法看到.
     * @param filePath
     */
    public  void systemToScan(String filePath) {
        //扫描指定文件夹中的文件
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File file = new File(filePath);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        //向系统发送广播,这里的作用域可以用application
        FrameApplication.getInstance().sendBroadcast(intent);
    }
}

4、在activity里面实现CopyDataBaseInterFace接口。XxxxActivity  implements CopyDataBaseInterFace。

5、运用

new Thread(new CopyDataBaseRunnable(XxxxActivity.this)).start() ;推荐使用线程池来运行

6、在回调的接口查看创建成功和失败。

猜你喜欢

转载自blog.csdn.net/baby986911/article/details/86685726