微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java Files.move 不起作用“不允许操作”

如何解决Java Files.move 不起作用“不允许操作”

我想使用 Files.move 重命名文件。通过测试,我可以看到该错误System.err: java.nio.file.filesystemexception: /storage/emulated/0/DCIM/Camera/20210726_110744.mp4 -> /storage/emulated/0/DCIM/Camera/svvshdd.mp4: Operation not permitted

我认为这与 Android 11 的范围存储相关联。但是,我创建了一个挂起的意图 createWriteRequest,所以我没有看到问题

这是我的活动:

@Override
public void renFile(Uri fileUri) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        ArrayList<Uri> fichiers2 = new ArrayList<>();
        fichiers2.add(fileUri);
        PendingIntent demande2 = MediaStore.createWriteRequest(this.getContentResolver(),fichiers2);
        try {
            ((Activity) this).startIntentSenderForResult(demande2.getIntentSender(),2,new Intent(),0);
        } catch (IntentSender.SendIntentException e) {
            e.printstacktrace();
        }
    }
}



@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    try {
        super.onActivityResult(requestCode,resultCode,data);

        if (requestCode == 1  && resultCode  == RESULT_OK) {

           videosAdapter.actualiserFichier();

        }

        if (requestCode == 2  && resultCode  == RESULT_OK) {

            videosAdapter.renommerFile();

        }
    } catch (Exception ex) {
        Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).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);
    ancienFile = new File(videoFolder.get(position).getPath());
    String nameText = ancienFile.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 = ancienFile.getParentFile().getAbsolutePath();
        String ext = ancienFile.getAbsolutePath();
        ext = ext.substring(ext.lastIndexOf("."));
        String newPath = onlyPath + "/" + editText.getText() + ext;
        File newFile = new File(newPath);


        ArrayList<Uri> fichiers = new ArrayList<>();
        Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,Long.parseLong(videoFolder.get(position).getId()));
        fichiers.add(contentUri);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (listener != null) {
                listener.renFile(contentUri);
                nouveauFile = newFile;
            }




       }else{
            boolean rename = ancienFile.renameto(newFile);
            if(rename){
                context.getApplicationContext().getContentResolver().delete(MediaStore.Files.getContentUri("external"),MediaStore.MediaColumns.DATA + "=?",new String[]{ancienFile.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,"Erreur,le fichier n'a pas été renommé",Snackbar.LENGTH_SHORT).show();
            }
        }


        dialog.dismiss();
    });
    dialog.show();

}

还有我的另一个空白:

 private void renommerFichier(File newFile,View view){

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Path source = Paths.get(ancienFile.getAbsolutePath());
        Path target = Paths.get(nouveauFile.getAbsolutePath());

        try{

           Files.move(source,target);
           System.out.println("C'est bien renommé");

        } catch (IOException e) {
            System.out.println("C'est pas renommé");
            e.printstacktrace();

        }
    }

我将所有这些都与 Interfaces 联系起来,而且效果很好。

我只是不明白为什么“不允许操作”

在我的清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
     />

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。