如何解决.delete() 和 renameTo() 在 Android 11 上不起作用 JAVA
我的重命名和删除功能不适用于 Android 11+。我听说过一种在 android 11 中管理权限的新方法,但我真的不知道如何使其适用于 Android 11+... 我看到了这个网站:https://developer.android.com/training/data-storage/use-cases#modify-delete-media
有我的空缺:
private void deleteFile(int p,View view){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Supprimer ?").setMessage(videoFolder.get(p).getTitle()).setNegativeButton("Annuler",(dialog,which) -> {
}).setPositiveButton("Oui",which) -> {
Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,Long.parseLong(videoFolder.get(p).getId()));
File file = new File(videoFolder.get(p).getPath());
boolean deleted = file.delete();
if (deleted){
context.getApplicationContext().getContentResolver().delete(contentUri,null,null);
videoFolder.remove(p);
notifyItemRemoved(p);
notifyItemRangeChanged(p,videoFolder.size());
Snackbar.make(view,"Fichier supprimé avec succès",Snackbar.LENGTH_SHORT).show();
}else{
Snackbar.make(view,"Erreur,le fichier n'a pas été supprimé",Snackbar.LENGTH_SHORT).show();
}
}).show();
}
private void renameFile(int position,View view){
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.rename_layout);
final EditText editText = dialog.findViewById(R.id.rename_edit_text);
Button cancel = dialog.findViewById(R.id.cancel_rename_button);
Button rename_btn = dialog.findViewById(R.id.rename_button);
final File renameFile = new File(videoFolder.get(position).getPath());
String nameText = renameFile.getName();
nameText = nameText.substring(0,nameText.lastIndexOf("."));
editText.setText(nameText);
editText.clearFocus();
dialog.getwindow().setSoftInputMode(WindowManager.LayoutParams.soFT_INPUT_STATE_ALWAYS_VISIBLE);
cancel.setonClickListener(v -> dialog.dismiss());
rename_btn.setonClickListener(v -> {
String onlyPath = renameFile.getParentFile().getAbsolutePath();
String ext = renameFile.getAbsolutePath();
ext = ext.substring(ext.lastIndexOf("."));
String newPath = onlyPath + "/" + editText.getText() + ext;
File newFile = new File(newPath);
boolean rename = renameFile.renameto(newFile);
if(rename){
context.getApplicationContext().getContentResolver().delete(MediaStore.Files.getContentUri("external"),MediaStore.MediaColumns.DATA + "=?",new String[]{renameFile.getAbsolutePath()});
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(newFile));
context.getApplicationContext().sendbroadcast(intent);
Snackbar.make(view,"Le fichier a bien été renommé",Snackbar.LENGTH_SHORT).show();
}else {
Snackbar.make(view,le fichier n'a pas été renommé",Snackbar.LENGTH_SHORT).show();
}
dialog.dismiss();
});
dialog.show();
}
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。