当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现复制Assets文件到SD卡

Android实现复制Assets文件到SD卡

2019年07月30日  | 移动技术网移动技术  | 我要评论

assets文件介绍

assets文件夹里面的文件都是保持原始的文件格式,需要用assetmanager以字节流的形式读取文件。

1. 先在activity里面调用getassets() 来获取assetmanager引用。
2. 再用assetmanager的open(string filename, int accessmode) 方法则指定读取的文件以及访问模式就能得到输入流inputstream。
3. 然后就是用已经open file 的inputstream读取文件,读取完成后记得inputstream.close() 。
4. 调用assetmanager.close() 关闭assetmanager。

封装类

代码遵循单例模式,例如:

import android.content.context;
import android.os.environment;
import android.os.handler;
import android.os.looper;
import android.os.message;

import java.io.file;
import java.io.fileoutputstream;
import java.io.inputstream;

/**
 * created by shenhua on 1/17/2017.
 * email shenhuanet@126.com
 */
public class fileutils {

 private static fileutils instance;
 private static final int success = 1;
 private static final int failed = 0;
 private context context;
 private fileoperatecallback callback;
 private volatile boolean issuccess;
 private string errorstr;

 public static fileutils getinstance(context context) {
  if (instance == null)
   instance = new fileutils(context);
  return instance;
 }

 private fileutils(context context) {
  this.context = context;
 }

 private handler handler = new handler(looper.getmainlooper()) {
  @override
  public void handlemessage(message msg) {
   super.handlemessage(msg);
   if (callback != null) {
    if (msg.what == success) {
     callback.onsuccess();
    }
    if (msg.what == failed) {
     callback.onfailed(msg.obj.tostring());
    }
   }
  }
 };

 public fileutils copyassetstosd(final string srcpath, final string sdpath) {
  new thread(new runnable() {
   @override
   public void run() {
    copyassetstodst(context, srcpath, sdpath);
    if (issuccess)
     handler.obtainmessage(success).sendtotarget();
    else
     handler.obtainmessage(failed, errorstr).sendtotarget();
   }
  }).start();
  return this;
 }

 public void setfileoperatecallback(fileoperatecallback callback) {
  this.callback = callback;
 }

 private void copyassetstodst(context context, string srcpath, string dstpath) {
  try {
   string filenames[] = context.getassets().list(srcpath);
   if (filenames.length > 0) {
    file file = new file(environment.getexternalstoragedirectory(), dstpath);
    if (!file.exists()) file.mkdirs();
    for (string filename : filenames) {
     if (!srcpath.equals("")) { // assets 文件夹下的目录
      copyassetstodst(context, srcpath + file.separator + filename, dstpath + file.separator + filename);
     } else { // assets 文件夹
      copyassetstodst(context, filename, dstpath + file.separator + filename);
     }
    }
   } else {
    file outfile = new file(environment.getexternalstoragedirectory(), dstpath);
    inputstream is = context.getassets().open(srcpath);
    fileoutputstream fos = new fileoutputstream(outfile);
    byte[] buffer = new byte[1024];
    int bytecount;
    while ((bytecount = is.read(buffer)) != -1) {
     fos.write(buffer, 0, bytecount);
    }
    fos.flush();
    is.close();
    fos.close();
   }
   issuccess = true;
  } catch (exception e) {
   e.printstacktrace();
   errorstr = e.getmessage();
   issuccess = false;
  }
 }

 public interface fileoperatecallback {
  void onsuccess();

  void onfailed(string error);
 }

}

调用代码

如果你需要将如图所示的apks下的文件复制到sd卡的app/apks目录下,则这样调用:

fileutils.getinstance(context context).copyassetstosd("apks","app/apks");

###如果你需要收到文件复制完成的时的回调,则使用如下代码:

fileutils.getinstance(context context).copyassetstosd("apks","app/apks").setfileoperatecallback(new fileutils.fileoperatecallback() {
 @override
  public void onsuccess() {
  // todo: 文件复制成功时,主线程回调
   }

   @override
   public void onfailed(string error) {
    // todo: 文件复制失败时,主线程回调
   }
  });

代码说明

在上面代码中,通过单例模式传入一个context获得fileutils实例,通过实例去调用copyassetstosd()方法,方法参数:

  • string srcpath 传入assets文件夹下的某个文件夹名,如上述apks,可传入为空”“字符,则复制到sd后,默认将assets文件夹下所有文件复制;
  • string sdpath 传入你希望将文件复制到的位置,如sd卡下的“abc”文件夹,则传入”abc”

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网