Saving Files

以文件的形式保存数据适合从文件开头读或者写到文件结束。例如将图片保存在文件中.
所有的Android 设定都分为内存和外存 路径来保存文件.
我们先看看如果在内存中保存文件
你可以通过下面两个函数来得到要保存文件的路径.
getFilesDir():来获取保存文件的路径.这个函数返回file 对象是保存在你app 内部的。
getCacheDir():文件保存在app的临时cache文件,当不需要火车size超过给定的size,需要删掉。当系统的memory不足时,保存在getCacheDir()路径下的文件会被删掉.
得到路径后要怎么产生新的文件呢?
File file = new File(context.getFilesDir(), filename);
你可以调用openFileOutput() 来得到一个FileOutPutStream,然后写文件到内部存储空间,下面是一个实际使用的例子.
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;


try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}
你如果需要缓存一些文件,可以通过下面的方式。
public File getTempFile(Context context, String url) {
    File file;
    try {
        String fileName = Uri.parse(url).getLastPathSegment();
        file = File.createTempFile(fileName, null, context.getCacheDir());
    } catch (IOException e) {
        // Error while creating file
    }
    return file;
}
如果在外存(例如SD卡)中保存文件.首先有在manifest文件中声明读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
要判断当前外存(SD)卡是否处于mount状态。
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}


/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}
如果你想把文件保存在外存的公共目录下,保存在公共目录下的是文件可以被其他app访问,即使你的app 被删掉,文件还是会得以保存,如下所示:
public File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory.
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}
如果你保存的文件,只想被你的app 访问到,可以采用下面的方法.
public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory.
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}


你可以通过gerFreeSpace或者getTotalSpace来得到free或者总的memory值,这样就可以控制你的文件的size,举例如下:


      final long freeBytes = path.getFreeSpace();
71            final long totalBytes = path.getTotalSpace();
72            final long usedBytes = totalBytes - freeBytes;
73
74            final String used = Formatter.formatFileSize(context, usedBytes);
75            final String total = Formatter.formatFileSize(context, totalBytes);
76            setSummary(context.getString(R.string.storage_volume_summary, used, total));
77            mUsedPercent = (int) ((usedBytes * 100) / totalBytes);


可以通过下面两种方式删掉文件.
myFile.delete();
mycontext.deleteFile(fileName)
def get_parser(): parser = argparse.ArgumentParser(description='Face detection and classification for politicians in Japanese TV.') # Important configuration variables parser.add_argument('--dataset', type=str, default='mot17', help='Mode name for saving files.') parser.add_argument('--mode', default='train', type=str, help='train or test.') parser.add_argument('--detector', type=str, default='YOLOX', help='Detector to be used. FRCNN, SDP, Bresee, SGT, YOLOX, GT.') parser.add_argument('--reid', type=str, default=None, help='Reidentification model to be used. SBS, MGN.') parser.add_argument('--mod', type=str, default=None, help='Tracker name modifier to do testing of features.') # Paths parser.add_argument('--datapath', type=str, default='datasets/MOT17Det', help='Dataset path with frames inside.') parser.add_argument('--feat', type=str, default='feats', help='Features files path.') # Tracking-specific configuration variables parser.add_argument('--max_iou_th', type=float, default=0.15, help='Max value to multiply the distance of two close objects.') parser.add_argument('--w_tracklet', type=int, default=10, help='Window size per tracklet') parser.add_argument('--w_fuse', type=int, default=3, help='Window size per fusion in hierarchy') parser.add_argument('--max_prop', type=int, default=10000, help='Difficult the fusion when the frame difference is larger than this value.') parser.add_argument('--fps_ratio', type=int, default=1, help='Use lower fps dataset if lower than 1.') # Flags parser.add_argument('--save_feats', action='store_true', help='Save tracking + feature vectors as pkl file for analysis.') parser.add_argument('--iou', action='store_true', help='Add IoU distance to further improve the tracker.') parser.add_argument('--temp', action='store_true', help='Use temporal distance to further improve the tracker.') parser.add_argument('--spatial', action='store_true', help='Use spatial distance to further improve the tracker.') parser.add_argument('--motion', action='store_true', help='Add motion estimation to further improve the tracker.') parser.add_argument('--randorder', action='store_true', help='Random order of lifted frames for testing.') parser.add_argument('--noncont', action='store_true', help='Do not enforce continuous clustering. Allow all tracklets to cluster with whoever they want.') return parser
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值