2

我正在尝试更新文档/文件的最后修改日期,但我收到“UnsupportedOperationException:不支持更新”

重现步骤:

  1. 选择文档树
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, 1972);
  1. 在 Activity Result 在所选目录中创建一个新文档:
Uri treeUri = resultData.getData();
String treeDocumentId = DocumentsContract.getTreeDocumentId(treeUri);
treeUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, treeDocumentId);
Uri uri = DocumentsContract.createDocument(getContentResolver(), treeUri, "text/plain", "test.txt");
  1. 尝试更新文档/文件的最后修改日期
ContentValues values = new ContentValues();
values.put(DocumentsContract.Document.COLUMN_LAST_MODIFIED, 1592143965000L);
getContentResolver().update(uri, values, null, null);

也尝试插入,但结果始终相同:

 Caused by: java.lang.UnsupportedOperationException: Update not supported
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
    at android.content.ContentProviderProxy.update(ContentProviderNative.java:578)
    at android.content.ContentResolver.update(ContentResolver.java:2009)

有没有人遇到过同样的问题,各自找到了解决这个问题的方法?

4

2 回答 2

1

您提到了范围存储。我正在寻找一种方法来更新通过Storage Access Framework复制/创建的文件的创建日期、修改日期等属性。这也需要使用DocumentsContract,DocumentFiles

根据文档所有列对客户端应用程序都是只读的

但是,我为更新创建的文件的属性所做的DocumentsContract.createDocument是将 Uri 转换为文件系统上的路径。然后以这种方式直接更新这些文件的属性:


// sourceUri & targetUri reference tree Uris

Path inFilePath = Paths.get(FileUtil.getFullDocIdPathFromTreeUri(sourceUri, context));
Path outFilePath = Paths.get(FileUtil.getFullDocIdPathFromTreeUri(targetUri, context));
BasicFileAttributes inAttrs = Files.readAttributes(inFilePath, BasicFileAttributes.class);
Files.getFileAttributeView(outFilePath, BasicFileAttributeView.class).setTimes(inAttrs.lastModifiedTime(), inAttrs.lastAccessTime(), inAttrs.creationTime());

FileUtil类(改编自:此 PullRequest https://github.com/nzbget/android/pull/12/files也引用了https://stackoverflow.com/a/36162691

package com......;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;

import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;

public final class FileUtil {
    private static final String PRIMARY_VOLUME_NAME = "primary";

    public static String getFullDocIdPathFromTreeUri(@Nullable final Uri treeUri, Context con) {
        if (treeUri == null) return null;
        String volumePath = getVolumePath(getVolumeIdFromTreeUri(treeUri),con);
        if (volumePath == null) return File.separator;
        if (volumePath.endsWith(File.separator))
            volumePath = volumePath.substring(0, volumePath.length() - 1);

        String documentPath = getDocumentPathFromTreeUri(treeUri);
        if (documentPath.endsWith(File.separator))
            documentPath = documentPath.substring(0, documentPath.length() - 1);

        if (documentPath.length() > 0) {
            if (documentPath.startsWith(File.separator))
                return volumePath + documentPath;
            else
                return volumePath + File.separator + documentPath;
        }
        else return volumePath;
    }

    @SuppressLint("ObsoleteSdkInt")
    private static String getVolumePath(final String volumeId, Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null;
        try {
            StorageManager mStorageManager =
                    (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
            Method getUuid = storageVolumeClazz.getMethod("getUuid");
            Method getPath = storageVolumeClazz.getMethod("getPath");
            Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
            Object result = getVolumeList.invoke(mStorageManager);

            final int length = Array.getLength(result);
            for (int i = 0; i < length; i++) {
                Object storageVolumeElement = Array.get(result, i);
                String uuid = (String) getUuid.invoke(storageVolumeElement);
                Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

                // primary volume?
                if (primary && PRIMARY_VOLUME_NAME.equals(volumeId))
                    return (String) getPath.invoke(storageVolumeElement);

                // other volumes?
                if (uuid != null && uuid.equals(volumeId))
                    return (String) getPath.invoke(storageVolumeElement);
            }
            // not found.
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static String getVolumeIdFromTreeUri(final Uri treeUri) {
        final String docId = DocumentsContract.getTreeDocumentId(treeUri);
        final String[] split = docId.split(":");
        if (split.length > 0) return split[0];
        else return null;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
        private static String getDocumentPathFromTreeUri(final Uri treeUri) {
        //final String docId = DocumentsContract.getTreeDocumentId(treeUri);
        final String docId = DocumentsContract.getDocumentId(treeUri);
        final String[] split = docId.split(":");
        if ((split.length >= 2) && (split[1] != null)) return split[1];
        else return File.separator;
    }
}

2021-03-30 编辑

如果您的 targetSDK >= 29,则在设置时间戳时可能会遇到“AccessDeniedException”。为避免这种情况,您可以查看 以下关于 stackoverflow的解决方案:

  • 对于 Android 10 支持,请将其android:requestLegacyExternalStorage="true"放在 Manifest 中。
  • 对于 Android 11,您需要 MANAGE_EXTERNAL_STORAGE 权限和 Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION(一些代码示例在github 提交中
于 2021-03-17T21:12:01.430 回答
0

在过去的 3 天里,我一直在通过 SAF 主题出汗。我的努力是将文件从 FTP 下载到 Android API28 上的外部 SD 卡)。我成功了,但是遇到了同样的问题,我无法将文件的 lastmodify 日期修改为 FTP 上的日期。我尝试了与上述相同的方法。有趣的是,我尝试了旧的 java file utils 方式

文件.setLastModified(l_lastModified);

令人惊讶的是,这有效!你不能用java文件工具写文件,你必须使用SAF,然后一旦文件下载得足够奇怪,你就可以修改这个文件的属性,这完全没有意义。然而,这解决了我的问题并且它有效。

于 2021-07-27T13:59:58.083 回答