AssetsUtils.java

/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at    http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.==============================================================================*/package com.lenovo.ailab.smartkit.utils;import android.content.Context;import android.content.res.AssetManager;import android.util.Log;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** Utilities for dealing with assets. */public class AssetUtils {  private static final String TAG = AssetUtils.class.getSimpleName();  private static final int BYTE_BUF_SIZE = 2048;  /**   * Copies a file from assets.   *   * @param context application context used to discover assets.   * @param assetName the relative file name within assets.   * @param targetName the target file name, always over write the existing file.   * @throws IOException if operation fails.   */  public static void copy(Context context, String assetName, String targetName) throws IOException {    Log.d(TAG, "creating file " + targetName + " from " + assetName);    File targetFile = null;    InputStream inputStream = null;    FileOutputStream outputStream = null;    try {      AssetManager assets = context.getAssets();      targetFile = new File(targetName);      if(!targetFile.getParentFile().exists()){        targetFile.getParentFile().mkdirs();      }      if(!targetFile.exists()){        targetFile.createNewFile();      }      inputStream = assets.open(assetName);      // TODO(kanlig): refactor log messages to make them more useful.      Log.d(TAG, "Creating outputstream");      outputStream = new FileOutputStream(targetFile, false /* append */);      copy(inputStream, outputStream);    } finally {      if (outputStream != null) {        outputStream.close();      }      if (inputStream != null) {        inputStream.close();      }    }  }  private static void copy(InputStream from, OutputStream to) throws IOException {    byte[] buf = new byte[BYTE_BUF_SIZE];    while (true) {      int r = from.read(buf);      if (r == -1) {        break;      }      to.write(buf, 0, r);    }  }}

调用实例:

        String conf = mContext.getExternalCacheDir().getPath() + "/deploy.rtttl";        String model =  mContext.getExternalCacheDir().getPath() + "/res10_300x300_ssd_iter_140000_fp16.rtttl";        String labelfile = mContext.getExternalCacheDir().getPath() + "/label.rtttl";        Log.i("FaceDetection", conf);        try {            AssetUtils.copy(mContext, "label.rtttl", labelfile);            AssetUtils.copy(mContext, "deploy.rtttl", conf);            AssetUtils.copy(mContext, "res10_300x300_ssd_iter_140000_fp16.rtttl", model);                 } catch (IOException e) {            e.printStackTrace();        }

 

更多相关文章

  1. Android对话框实例-注册对话框
  2. Android 全屏显示实例
  3. android之Fragment静态实现实例
  4. android用户界面详尽教程实例
  5. [置顶] 我的Android进阶之旅------>Android Widget 桌面数字时钟
  6. Android中判断网络是否连接实例详解
  7. Android 实现扫雷小游戏实例代码
  8. Android——ViewGroup的一个用法实例
  9. android APP隐私政策弹框的实现代码实例

随机推荐

  1. android 调用系统应用
  2. ch02 Android TextView与EditView
  3. android studio更新到1.5后遇到的问题
  4. android实现调用系统音乐播放器
  5. android 两点缩放字体
  6. 九宫格
  7. Android 2D绘图总结
  8. android WebView java与js相互调用
  9. EditText对行光标默认第一行问题
  10. Android 中的 R.class,减小 Apk 包大小