gpt4 book ai didi

java - 针对 Android 11 (Api 30) 重命名视频/图像

转载 作者:行者123 更新时间:2023-12-04 23:50:32 25 4
gpt4 key购买 nike

我很难简单地重命名由应用程序创建但被放入文档文件夹的文件。
编辑:
碰巧视频不是由应用程序创建的,而是由应用程序重命名的。用户在开始时手动将视频放入文档文件夹中。我的错。
这是我的代码:

public static boolean renameVideoFile(Context c, File from, File to) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Uri fromUri = FileProvider.getUriForFile(c, c.getPackageName() + ".provider", new File(FileUtils.getVideosDir(), from.getName()));
ContentResolver contentResolver = c.getContentResolver();
ContentValues contentValues = new ContentValues();

contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, to.getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} else {
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
}
我经历了一些错误,但我的最后一个错误是:
java.lang.UnsupportedOperationException:没有外部更新
这是 FileProvider 的内部问题
在 androidx.core.content.FileProvider.update(FileProvider.java:523)
编辑#2
这里还有我在 list 中的提供者声明:
 <provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
这是我的路径声明。同样,这不会导致保存问题:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="internal_images"
path="files/Pictures" />
<external-files-path
name="internal_images_alternate"
path="Pictures" />
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>

最佳答案

编辑:我选择的外部文件夹是文档文件夹仅供引用
所以我终于让它工作了。这是重命名视频的代码(它可能不是最好的,但可以解决问题!)

private static void tryAddVideosToMediaStore(Activity context) {
List<File> files = MediaUtils.getVideoFilesFromDirectory();
for (File file : files) {
try {
Uri fromUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);

if (getRealPathFromURI(context, fromUri) == null) {

String nameWoExtension = MediaUtils.getNameWithoutStatus(file.getAbsolutePath());
ContentValues values = new ContentValues(3);
values.put(MediaStore.Video.Media.TITLE, nameWoExtension);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch(Exception e) {
return null;
}finally {
if (cursor != null) {
cursor.close();
}
}
}
然后调用方法
public static String getVideoNameFromPath(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.indexOf(".mp4"));
}

public static boolean renameVideoFile(MainActivityViewModel viewModel, SharedPreferenceHelper sharedPreferenceHelper, Activity c, File from, File to) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
tryAddVideosToMediaStore(c);
Uri fromUri = MediaUtils.getVideoUriFromFS(c, from);
try {
ContentResolver contentResolver = c.getContentResolver();
ContentValues contentValues = new ContentValues();

contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, to.getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
return true;
} catch (Exception securityException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
sharedPreferenceHelper.get().edit().putString("from", from.getAbsolutePath()).putString("to", to.getAbsolutePath()).apply();
RecoverableSecurityException recoverableSecurityException;
viewModel.setContentUri(fromUri);

if (securityException instanceof RecoverableSecurityException) {
recoverableSecurityException =
(RecoverableSecurityException) securityException;
} else {
requestVideoWritePermissions(c, Uri.parse(MediaStore.Video.Media.EXTERNAL_CONTENT_URI + "/" + MediaUtils.getVideoId(c, from)));
return false;
}
IntentSender intentSender = recoverableSecurityException.getUserAction()
.getActionIntent().getIntentSender();

try {
c.startIntentSenderForResult(intentSender, 55,
null, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
throw new RuntimeException(
securityException.getMessage(), securityException);
}
}
return false;
} else {
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
}


public static Uri getVideoUriFromFS(Context c, File file) {
long id = getFilePathToMediaID(file, c);
Uri fromUri = ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI,id);
return fromUri;
}



public static long getFilePathToMediaID(File videoPath, Context context)
{
Uri mainUri;
Cursor cursor1 = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID},
MediaStore.Video.Media.DATA + "=? ",
new String[]{videoPath.getAbsolutePath()}, null);
long id = 0;
if (cursor1 != null && cursor1.moveToFirst()) {

id = cursor1.getLong(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
cursor1.close();
}
return id;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 55) { //rename video request code
if (resultCode == RESULT_OK) {
//update UI
String from = presenter.getFromFilePath();
String to = presenter.getToFilePath();
if (from != null && to != null) {
Uri fromUri = MediaUtils.getVideoUriFromFS(this, new File(from));
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();

contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, new File(to).getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
//update UI
}
}
}
}
如果我忘记了什么,请告诉我,我会在这里发布。确实花了几个小时的搜索才能找到这个解决方案。我对谷歌引入的简单性和复杂性感到非常不安。
编辑:我想我忘记了这个非常重要的方法
public static boolean requestVideoWritePermissions(Activity activity, Uri fromUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

boolean hasPermission = true;
if (activity.checkUriPermission(fromUri, Binder.getCallingPid(), Binder.getCallingUid(),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
hasPermission = false;
}

List<Uri> uriList = new ArrayList<>();
uriList.add(fromUri);

if (!hasPermission) {
PendingIntent pi = MediaStore.createWriteRequest(activity.getContentResolver(), uriList);
try {
activity.startIntentSenderForResult(pi.getIntentSender(), 55, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
return false;
}
return true;
}
return true;
}
我还应该提到每个视频都是这样的提示。用户选择是否允许您覆盖每个不是最佳的视频。我希望我可以只做一个外部访问的整个文件夹,但我猜这不会发生在范围存储更改中。

关于java - 针对 Android 11 (Api 30) 重命名视频/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68641037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com