Android中将assets中的文件拷贝到sd卡

本文介绍了一个名为AssetsUtils的Java工具类,该类提供了一种从Android应用的assets文件夹中复制文件到设备上的指定路径的方法。通过使用此工具类,开发者可以轻松地将预置的模型文件、配置文件等资源复制到应用运行时所需的目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations 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();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值