如何解决无法在我的 Android 应用程序中重命名和删除我的 pdf 文件
我无法使用 renameto() 方法重命名和删除 pdf 阅读器应用程序中的 pdf 文件,并且 file.delete() 方法也无法从内部存储中删除 pdf 文件。 我附上了我的 Android 工作室中所有可能有助于找到问题的相关代码:
我的 RecyclerAdapter 代码,我试图根据 MenuItem 选择重命名和删除文件:
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
int position = getBindingAdapterPosition();
File file=pdf.get(position);
file.delete();
pdf.remove(position);
notifyItemChanged(position);
notifyItemRangeChanged(position,pdf.size());
return true;
case R.id.menu_rename:
AlertDialog.Builder jumpto = new AlertDialog.Builder(mcontext);
jumpto.setTitle("Rename");
EditText page = new EditText(mcontext);
page.setText(pdfname.getText());
jumpto.setView(page);
jumpto.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
File old = new File(pdf.get(getBindingAdapterPosition()).getAbsolutePath());
String name = old.getParentFile().getAbsolutePath();
String newpath = name + page.getText().toString();
File newfile = new File(newpath);
Boolean rename = old.renameto(newfile);
if (rename) {
ContentResolver resolver = mcontext.getContentResolver();
resolver.delete(MediaStore.Files.getContentUri("external"),MediaStore.MediaColumns.DATA + "=?",new
String[]{
old.getAbsolutePath()});
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(newfile));
mcontext.getApplicationContext().sendbroadcast(intent);
}
}
});
protected List<File> getPdfList() {
Uri collection;
final String[] projection = new String[]{
MediaStore.Files.FileColumns.disPLAY_NAME,MediaStore.Files.FileColumns.DATE_ADDED,MediaStore.Files.FileColumns.DATA,MediaStore.Files.FileColumns.MIME_TYPE,};
final String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
final String[] selectionArgs = new String[]{mimeType};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
}else{
collection = MediaStore.Files.getContentUri("external");
}
try (Cursor cursor =getContext().getApplicationContext().getContentResolver().query(collection,projection,selection,selectionArgs,sortOrder)) {
assert cursor != null;
if (cursor.movetoFirst()) {
int columnData = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int columnName = cursor.getColumnIndex(MediaStore.Files.FileColumns.disPLAY_NAME);
String path=cursor.getString(columnData);
if(new File(path).exists()){
do {
pdf.add(new File((cursor.getString(columnData))));
// Log.d(TAG,"getPdf: " + cursor.getString(columnData));
//you can get your pdf files
} while (cursor.movetoNext());
}
}
}
return pdf;
}
这是我请求运行时权限的 MainActivity.java 代码:
if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED&&ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
} else {
requestStoragePermission();
}
}
int Request_Code = 12;
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestStoragePermission() {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},Request_Code);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if (requestCode == Request_Code) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
} else {
}
解决方法
你可以检查你的文件是否被删除了你可以检查这可能对你有帮助这里是删除代码
boolean b = documentList.get(position).getFile().delete();
if (b) {
documentList.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
Toast.makeText(context,"File Deleted!",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,"File Deletion Failed!",Toast.LENGTH_SHORT).show();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。