• 软工划水日报-模型导入文件utils 4/29


    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;
        }
    }

    通过这个类可以将模型推进缓存

  • 相关阅读:
    用c写一个小的聊天室程序
    socket相关的开机初始化分析
    HTML——CSS3学习
    iOS--OCR图片识别
    iOS学习——Socket
    iOS学习——数据加密
    iOS学习——并发编程GCD
    iOS学习——weak的应用场景
    iOS学习——RUNLOOP、NSTimer
    iOS学习——锁
  • 原文地址:https://www.cnblogs.com/Sakuraba/p/14910320.html
Copyright © 2020-2023  润新知