最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)復(fù)制Assets文件到SD卡

 更新時間:2018年12月21日 10:36:43   作者:神話2009  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Assets文件介紹

assets文件夾里面的文件都是保持原始的文件格式,需要用AssetManager以字節(jié)流的形式讀取文件。

1. 先在Activity里面調(diào)用getAssets() 來獲取AssetManager引用。
2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。
3. 然后就是用已經(jīng)open file 的inputStream讀取文件,讀取完成后記得inputStream.close() 。
4. 調(diào)用AssetManager.close() 關(guān)閉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);
 }

}

調(diào)用代碼

如果你需要將如圖所示的apks下的文件復(fù)制到SD卡的app/apks目錄下,則這樣調(diào)用:

FileUtils.getInstance(Context context).copyAssetsToSD("apks","app/apks");

###如果你需要收到文件復(fù)制完成的時的回調(diào),則使用如下代碼:

FileUtils.getInstance(Context context).copyAssetsToSD("apks","app/apks").setFileOperateCallback(new FileUtils.FileOperateCallback() {
 @Override
  public void onSuccess() {
  // TODO: 文件復(fù)制成功時,主線程回調(diào)
   }

   @Override
   public void onFailed(String error) {
    // TODO: 文件復(fù)制失敗時,主線程回調(diào)
   }
  });

代碼說明

在上面代碼中,通過單例模式傳入一個context獲得FileUtils實例,通過實例去調(diào)用copyAssetsToSD()方法,方法參數(shù):

  • String srcPath 傳入assets文件夾下的某個文件夾名,如上述apks,可傳入為空”“字符,則復(fù)制到SD后,默認(rèn)將assets文件夾下所有文件復(fù)制;
  • String sdPath 傳入你希望將文件復(fù)制到的位置,如SD卡下的“abc”文件夾,則傳入”abc”

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

昌乐县| 兴文县| 旺苍县| 扬州市| 台中市| 诸城市| 五家渠市| 洮南市| 湖口县| 略阳县| 克拉玛依市| 盱眙县| 东山县| 云梦县| 光山县| 福州市| 苍山县| 邢台市| 罗平县| 辉县市| 黑河市| 喀喇| 洛扎县| 曲麻莱县| 高雄市| 汶上县| 旅游| 安顺市| 鲜城| 松潘县| 五常市| 三原县| 苏尼特右旗| 西丰县| 峨眉山市| 龙泉市| 金塔县| 武鸣县| 措勤县| 唐海县| 崇仁县|