如何解决Android:作用域存储:getContentResolverupdate...:COLUMN_LAST_MODIFIED:UnsupportedOperationException:不支持更新
我正在尝试更新文档/文件的上次修改日期,但是我收到“ UnsupportedOperationException:不支持更新”
复制步骤:
- 选择文档树
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);
- 关于活动结果,在所选目录中创建一个新文档:
Uri treeUri = resultData.getData();
String treeDocumentId = DocumentsContract.getTreeDocumentId(treeUri);
treeUri = DocumentsContract.buildDocumentUriUsingTree(treeUri,treeDocumentId);
Uri uri = DocumentsContract.createDocument(getContentResolver(),treeUri,"text/plain","test.txt");
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)
解决方法
您提到了作用域存储。我一直在寻找一种方法来更新通过存储访问框架复制/创建的文件的某些属性,例如创建日期、修改日期...。
这还需要使用 DocumentsContract
、DocumentFiles
根据doc,所有列对客户端应用程序都是只读的
但是,我为更新使用 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”。为避免这种情况,您可以查看以下总结的解决方案 here on stackoverflow:
- 对于 Android 10 支持,将此
android:requestLegacyExternalStorage="true"
放在清单中。 - 对于 Android 11,您需要 MANAGE_EXTERNAL_STORAGE 权限和 Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION(一些代码示例在 a github commit 中
在过去 3 天里,我一直在为 SAF 主题出汗。我的努力是将文件从 FTP 下载到 Android API28 上的外部 SD 卡)。我成功执行此操作但是面临同样的问题,即我无法将文件的最后修改日期修改为 FTP 上的日期。我尝试了与上述相同的方法。 有趣的是,我尝试了使用
file.setLastModified(l_lastModified);
令人惊讶的是这有效!你不能用java文件utils写文件,你必须使用SAF,然后一旦文件被奇怪地下载了你就可以修改这个文件的属性,这完全没有意义。但是,这解决了我的问题并且有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。