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 sms function send function
  4. android开机启动代码
  5. Unity3d 调用Android震动
  6. Android之Button样式
  7. Android(安卓)中 上下文Context理解
  8. Android调用第三方程序
  9. Android(安卓)AESUtils 工具类与调用方法

随机推荐

  1. andorid 自动化测试初探
  2. android两端对齐
  3. Android手写优化-更为平滑的签名效果实现
  4. 全面了解Android开发规范
  5. Android(安卓)程序之在线词典[2010-05-08
  6. Handler消息处理机制
  7. 如何在WebView中建立Android(安卓)Apps
  8. Android(安卓)6.0 SSL通信
  9. 第三方推送已死
  10. Android加壳native实现