Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fix the issue where the last modified time of a file is lost after selecting the file. #1649

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public void onCancel(final Object arguments) {
activityBinding.addActivityResultListener(this.delegate);
this.lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
this.lifecycle.addObserver(this.observer);

}

private void tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ public static FileInfo openFileStream(final Context context, final Uri uri, bool

final File file = new File(path);

// getLastModified
long fileTime = 0L;
try {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
if (columnIndex != -1 && cursor.moveToFirst()) {
fileTime = cursor.getLong(columnIndex);
}
cursor.close();
}
} catch (Exception e) {
Log.e(TAG, "Error getting SAF file time", e);
}

if(!file.exists()) {
file.getParentFile().mkdirs();
try {
Expand All @@ -313,6 +328,15 @@ public static FileInfo openFileStream(final Context context, final Uri uri, bool
} finally {
fos.getFD().sync();
}

// setLastModified
if (fileTime != 0 && file.exists()) {
boolean success = file.setLastModified(fileTime);
if (!success) {
Log.w(TAG, "Failed to set last modified time for file: " + file.getAbsolutePath());
}
}

} catch (final Exception e) {
try {
fos.close();
Expand Down Expand Up @@ -479,4 +503,4 @@ private static void recursiveDeleteFile(final File file) throws Exception {
file.delete();
}

}
}