I'm migrating my app to target it on android 11. I'm downloading a file and saving it to the folder: " Donwnload
" through the MediaStore
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
contentValues.put(
MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS );
uri = resolver.insert(MediaStore.Files.getContentUri("external"), contentValues);
However when downloading the file again he should delete the old one and download the new one.
In the past it used the following code:
if (file.exists ())
file.delete ();
But now with android 11 I can't do that and every time I download the file it adds it at the end of the file (1), (2), (3) and so on
Is there a way to delete the file or replace it?
// Create class FileUtils
public class FileUtils {
public final static String DEFAULT_TEMP = "/.Temp/";
public static String getPathFromUri(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public static String formatFileSize(long fileSize) {
DecimalFormat df = new DecimalFormat("0.00");
String fileSizeString;
if (fileSize <= 0) {
fileSizeString = "0KB";
} else if (fileSize < (1024 * 1024)) {
fileSizeString = df.format((double) fileSize / 1024) + "KB";
} else if (fileSize < (1024 * 1024 * 1024)) {
fileSizeString = df.format((double) fileSize / (1024 * 1024)) + "MB";
} else {
fileSizeString = df.format((double) fileSize / (1024 * 1024 * 1024)) + "GB";
}
return fileSizeString;
}
public static String getMimeType(Uri uri) {
String mimeType = null;
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
ContentResolver cr = App.getInstance().getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
public static String getFileExtensionNoPoint(String path) {
if (TextUtils.isEmpty(path)) {
return "";
}
return getFileExtensionNoPoint(new File(path));
}
private static String getFileExtensionNoPoint(File file) {
if (file == null || file.isDirectory()) {
return "";
}
String fileName = file.getName();
if (fileName != null && fileName.length() > 0) {
int lastIndex = fileName.lastIndexOf('.');
if ((lastIndex > -1) && (lastIndex < (fileName.length() - 1))) {
return fileName.substring(lastIndex + 1);
}
}
return "";
}
private static boolean isCompareFiles(String path1, String path2) {
if (TextUtils.isEmpty(path1) || TextUtils.isEmpty(path2)) {
return false;
}
if (path1.equalsIgnoreCase(path2)) {
return true;
} else {
return isCompareFiles(new File(path1), new File(path2));
}
}
private static boolean isCompareFiles(File file1, File file2) {
if (file1 == null || file2 == null) {
return false;
}
if (file1.getPath().equalsIgnoreCase(file2.getPath())) {
return true;
}
return false;
}
private static boolean isSDExist() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
public static String getSDCardFilesPath() {
if (!isSDExist()) {
return "";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
}
private static String getSDCardDownloadPath() {
if (!isSDExist()) {
return "";
}
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/";
}
public static String getFileNameNoExtension(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return "";
}
return getFileNameNoExtension(new File(filePath));
}
public static String getFileNameNoExtension(File file) {
if (file == null) {
return "";
}
String filename = file.getName();
if (!TextUtils.isEmpty(filename)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
private static boolean isFileExist(String path) {
try {
if (!TextUtils.isEmpty(path)) {
File file = new File(path);
return file.exists();
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private static String getFileMimeTypeFromExtension(String fileType) {
try {
if (TextUtils.isEmpty(fileType)) {
return "*/*";
}
fileType = fileType.replace(".", "");
if (fileType.equalsIgnoreCase("docx") || fileType.equalsIgnoreCase("wps")) {
fileType = "doc";
} else if (fileType.equalsIgnoreCase("xlsx")) {
fileType = "xls";
} else if (fileType.equalsIgnoreCase("pptx")) {
fileType = "ppt";
}
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
if (mimeTypeMap.hasExtension(fileType)) {
return mimeTypeMap.getMimeTypeFromExtension(fileType);
}
} catch (Exception e) {
e.printStackTrace();
}
return "*/*";
}
public static String getAppTempPath() {
return getAvailableFilesPathAndroidData(true) + DEFAULT_TEMP;
}
private static String getAvailableFilesPathAndroidData(boolean boolToCache) {
if (!isSDExist()) {
if (boolToCache) {
return App.getInstance().getCacheDir().getAbsolutePath() + "/";
}
return App.getInstance().getFilesDir().getAbsolutePath() + "/";
} else {
if (boolToCache) {
return App.getInstance().getExternalCacheDir().getAbsolutePath() + "/";
}
return App.getInstance().getExternalFilesDir("").getAbsolutePath() + "/";
}
}
public static void deleteFileAndroid11(AppCompatActivity activity, Media media, ActivityResultLauncher<IntentSenderRequest> launcher) {
File file = new File(media.getPath());
Uri uri = Uri.fromFile(file);
ContentResolver contentResolver = activity.getContentResolver();
PendingIntent pendingIntent = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ArrayList<Uri> collection = new ArrayList<>();
String mineType = getMimeType(uri);
Uri contentUri = MediaStore.Images.Media.getContentUri("external");
boolean isVideo = mineType.contains("video");
if (isVideo) {
contentUri = MediaStore.Video.Media.getContentUri("external");
}
boolean isAudio = mineType.contains("audio");
if (isAudio) {
contentUri = MediaStore.Audio.Media.getContentUri("external");
}
collection.add(ContentUris.withAppendedId(contentUri, media.getMediaId()));
pendingIntent = MediaStore.createDeleteRequest(contentResolver, collection);
}
if (pendingIntent != null) {
IntentSender sender = pendingIntent.getIntentSender();
IntentSenderRequest request = new IntentSenderRequest.Builder(sender).build();
launcher.launch(request);
}
}
public static ActivityResultLauncher<IntentSenderRequest> requestLauncher(AppCompatActivity activity, OnActionCallback callback) {
return activity.registerForActivityResult(
new ActivityResultContracts.StartIntentSenderForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
callback.callback("");
}
});
}
}
// Declare launcher in class perform delete
private final ActivityResultLauncher<IntentSenderRequest> launcher;
// The delete method drops into your delete function
launcher = FileUtils.requestLauncher((BaseActivity) context, (key, data) -> {
mList.remove(index);
notifyItemRemoved(index);
});
FileUtils.deleteFileAndroid11((BaseActivity) context, media, launcher);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.