public class Utils { // 复制莫模型文件到缓存目录 public static void copyFileFromAsset(Context context, String oldPath, String newPath) { try { // 预测模型文件在assets中的位置 String[] fileNames = context.getAssets().list(oldPath); if (fileNames.length > 0) { // directory File file = new File(newPath); if (!file.exists()) { file.mkdirs(); } // copy recursivelyC for (String fileName : fileNames) { copyFileFromAsset(context, oldPath + "/" + fileName, newPath + "/" + fileName); } } else { // file File file = new File(newPath); // if file exists will never copy if (file.exists()) { return; } // copy file to new path InputStream is = context.getAssets().open(oldPath); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int byteCount; while ((byteCount = is.read(buffer)) != -1) { fos.write(buffer, 0, byteCount); } fos.flush(); is.close(); fos.close(); } } catch (Exception e) { e.printStackTrace(); } } // start camera public static Uri start_camera(Activity activity, int requestCode) { Uri imageUri; // save image in cache path File outputImage = new File(activity.getExternalCacheDir(), "out_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (Build.VERSION.SDK_INT >= 24) { // compatible with Android 7.0 or over imageUri = FileProvider.getUriForFile(activity,"com.example.ironfarm.fileprovider", outputImage); } else { imageUri = Uri.fromFile(outputImage); } // set system camera Action Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // set save photo path intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set photo quality, min is 0, max is 1 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); activity.startActivityForResult(intent, requestCode); return imageUri; } }
通过这个类可以将模型推进缓存