Android:获取私有文件的lastModified

Dav*_*uel 4 android file last-modified

我试图获取内部文件的最后修改日期,它总是返回0,虽然我可以正确读取文件.如果我做错了什么,你能告诉我吗?

创建文件......

FileOutputStream Os = activity.openFileOutput("file1.jpg",Context.MODE_PRIVATE);
cachedImage.compress(CompressFormat.JPEG,75,Os);
Os.close();

阅读文件......

long mod_time = 0;
File file = new File("file1".jpg");
mod_time = file.lastModified();
FileInputStream Is = activity.openFileInput("file1.jpg");
cachedImage = BitmapFactory.decodeStream(Is);
Is.close() ;

mod_time总是零!!!!

yan*_*nko 5

应该

File file = new File(activity.getFilesDir().getAbsolutePath() + "/file1.jpg");
Run Code Online (Sandbox Code Playgroud)

这就是创建文件的地方.
简单检查:

File file = new File("file1.jpg");
Log.e("x", file.getAbsolutePath());
// /file1.jpg
Log.e("x", "" + file.exists());
// false
Run Code Online (Sandbox Code Playgroud)