How to Copy Image File from Gallery to another folder without changing its modified date programmatically?

huangapple go评论68阅读模式
英文:

How to Copy Image File from Gallery to another folder without changing its modified date programmatically?

问题

private void copyFile(File sourceFile, File destFile){
    try (FileOutputStream fos = new FileOutputStream(destFile)) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Files.copy(sourceFile.toPath(), fos);
        }

    } catch (FileNotFoundException e) {
        Log.d("Tag", e.getMessage());
    } catch (Exception e) {
        Log.d("Tag", e.getMessage());
    }
}

Note: The code part has been translated, and I've removed the request not to provide additional content or answer translation-related questions.

英文:
 private void copyFile(File sourceFile, File destFile){
        try (FileOutputStream fos = new FileOutputStream(destFile)) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Files.copy(sourceFile.toPath(), fos);
            }

        } catch (FileNotFoundException e) {
            Log.d("Tag",e.getMessage());
        } catch (Exception e) {
            Log.d("Tag", e.getMessage());
        }
    }

I tried above code copy paste working but modified date is also changed and I don't want to change modified date.

答案1

得分: 1

Date date = new Date(files.get(i).lastModified());

new File(outputPath + "/" + inputFile).setLastModified(newDate.getTime());

首先获取复制前的最后修改日期,然后在粘贴后将该最后修改日期应用于新文件。

英文:
 Date date = new Date(files.get(i).lastModified());

new File(outputPath + "/" + inputFile).setLastModified(newDate.getTime());

first get last modified date before copy and after paste apply that last modified date to new file

huangapple
  • 本文由 发表于 2020年1月3日 19:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定