android - 无法将原始文件复制到临时文件

标签 android camera exif

当我在我的应用中拍照时, 我的应用程序在照片的 exif 元数据中保存了纬度、经度信息。

在 Galaxy Note 10+ 中,“exif.saveAttributes()”抛出 IOException。 但是我不知道它是怎么出现的。

这是我的代码。

try {
    double latitude  = data.getDoubleExtra("latitude", 38);
    double longitude = data.getDoubleExtra("longitude", 128);

    String strLatitude  = convertTagGPSFormat(latitude);
    String strLongitude = convertTagGPSFormat(longitude);

    ExifInterface exif = new ExifInterface(imageFilePath);
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, strLatitude);
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, strLongitude);
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
    exif.saveAttributes();
    Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
    isCreatedFile = false;
} catch (Exception e) {
    isCreatedFile = false;
    Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show();
    e.printStackTrace();
}

2021-02-25 10:13:54.456 7623-7623/com.example.whalemonitorapp W/System.err: java.io.IOException: Failed to copy original file to temp file
2021-02-25 10:13:54.458 7623-7623/com.example.whalemonitorapp W/System.err:     at android.media.ExifInterface.saveAttributes(ExifInterface.java:2098)
2021-02-25 10:13:54.459 7623-7623/com.example.whalemonitorapp W/System.err:     at com.example.whalemonitorapp.MainActivity.onActivityResult(MainActivity.java:439)
2021-02-25 10:13:54.459 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:8310)
2021-02-25 10:13:54.459 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
2021-02-25 10:13:54.459 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.os.Looper.loop(Looper.java:223)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-02-25 10:13:54.460 7623-7623/com.example.whalemonitorapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-02-25 10:13:54.461 7623-7623/com.example.whalemonitorapp W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-02-25 10:13:54.461 7623-7623/com.example.whalemonitorapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-02-25 10:13:54.463 7623-7623/com.example.whalemonitorapp W/System.err: Caused by: java.io.IOException: Couldn't rename to /storage/emulated/0/Pictures/Whale/WHALE_20210225_101347639105132634869099933.jpg.tmp
2021-02-25 10:13:54.463 7623-7623/com.example.whalemonitorapp W/System.err:     at android.media.ExifInterface.saveAttributes(ExifInterface.java:2084)
2021-02-25 10:13:54.463 7623-7623/com.example.whalemonitorapp W/System.err:     ... 14 more

我没有重命名任何文件,但由于重命名临时文件而失败。 我该如何解决这个问题?

最佳答案

参见 the answer here .较新版本的 Android 操作系统正在“监管”在 DCIM 和图片等公共(public)文件夹中创建文件。当 EXIFInterface 尝试创建一个扩展名为“.tmp”且仅允许使用“.jpg”、“.png”或“.mp4”的文件时,它会失败。

关于android - 无法将原始文件复制到临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66361129/

相关文章:

video - 以毫秒精度获取mp4视频的开始时间

Android TableRow child 问题

javascript - React-Native 应用程序上的 AppLock 功能

android - 如何以编程方式在 PercentRelativeLayout 中设置 percentHeight

Android 使用渲染脚本将 ImageReader Image 转换为 YCbCr_420_SP (NV21) 字节数组?

java - 如何使用相机获取一个物体到另一个物体的距离?

ios - 制作一个将 UIImage 保存到照片库的方法

php - 如何编写标题、评论、标签等图像数据

android - 在 oncreate 或 onresume 中获取 menuitem

Android:从图像中获取所有现有的 exif 数据