Java Code Examples for android.support.v4.provider.DocumentFile#delete()

The following examples show how to use android.support.v4.provider.DocumentFile#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From Rey-MusicPlayer with Apache License 2.0 6 votes vote down vote up
public static boolean deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        if (fileOrDirectory.listFiles() != null)
            for (File child : fileOrDirectory.listFiles()) {
                return deleteRecursive(child);
            }

    if (MusicUtils.isKitkat() || !MusicUtils.isFromSdCard(fileOrDirectory.getAbsolutePath())) {
        if (fileOrDirectory.delete()) {
            return MusicUtils.deleteViaContentProvider(fileOrDirectory.getAbsolutePath());
        }

    } else {
        DocumentFile documentFile = getDocumentFile(fileOrDirectory);
        if (documentFile.delete()) {
            return MusicUtils.deleteViaContentProvider(fileOrDirectory.getAbsolutePath());
        }
    }
    return false;
}
 
Example 2
Source File: FileUtils.java    From Rey-MusicPlayer with Apache License 2.0 6 votes vote down vote up
public static boolean deleteFile(File file) {
    if (file == null) {
        throw new NullPointerException("File can't be null");
    }
    if (MusicUtils.isFromSdCard(file.getAbsolutePath())) {
        DocumentFile documentFile = getDocumentFile(file);
        if (documentFile.isDirectory()) {
            deleteDirectory(documentFile);
        } else {
            Logger.log("Deleted File Name  " + file.getName());
            return documentFile.delete();
        }
    } else {
        return file.delete();
    }
    return false;
}
 
Example 3
Source File: FileUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Delete a file. May be even on external SD card.
  *
  * @param file the file to be deleted.
  * @return True if successfully deleted.
  */
 static boolean deleteFile(@NonNull final File file, Context context) {
     // First try the normal deletion.
     if (file == null || !file.exists()) 
return true;
     if (file.delete() || deleteFilesInFolder(file, context))
         return true;

     // Try with Storage Access Framework.
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && FileUtil.isOnExtSdCard(file, context)) {
         final DocumentFile document = getDocumentFile(file, false, context);
if (document != null) {
	return document.delete();
}
     }

     // Try the Kitkat workaround.
     if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
         final ContentResolver resolver = context.getContentResolver();

         try {
             final Uri uri = MediaStoreHack.getUriFromFile(file.getAbsolutePath(), context);
             resolver.delete(uri, null, null);
             return !file.exists();
         } catch (Exception e) {
             Log.e("AmazeFileUtils", "Error when deleting file " + file.getAbsolutePath(), e);
             return false;
         }
     }

     return !file.exists();
 }
 
Example 4
Source File: AndroidPathUtils.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static boolean deleteFile(@NonNull final File file, final Context c) {
	// First try the normal deletion.
	if (file.delete()) {
		return true;
	}

	// Try with Storage Access Framework.
	if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
		DocumentFile document = getDocumentFile(file, false, false, c);
		return document != null && document.delete();
	}

	// Try the Kitkat workaround.
	if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
		ContentResolver resolver = c.getContentResolver();

		try {
			Uri uri = getUriFromFile(file.getAbsolutePath(), c);
			if (uri != null) {
				resolver.delete(uri, null, null);
			}
			return !file.exists();
		}
		catch (Exception e) {
			Log.e("Uri", "Error when deleting file " + file.getAbsolutePath(), e);
			return false;
		}
	}

	return !file.exists();
}
 
Example 5
Source File: AndroidPathUtils.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Delete a folder.
 *
 * @param file The folder name.
 * @return true if successful.
 */
public static boolean rmdir(@NonNull final File file, final Context c) {
	if (!file.exists()) {
		return true;
	}
	if (!file.isDirectory()) {
		return false;
	}
	String[] fileList = file.list();
	if (fileList != null && fileList.length > 0) {
		// Delete only empty folder.
		return false;
	}

	// Try the normal way
	if (file.delete()) {
		return true;
	}

	// Try with Storage Access Framework.
	if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
		DocumentFile document = getDocumentFile(file, true, true, c);
		return document != null && document.delete();
	}

	// Try the Kitkat workaround.
	if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
		ContentResolver resolver = c.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
		resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

		// Delete the created entry, such that content provider will delete the file.
		resolver.delete(MediaStore.Files.getContentUri("external"), MediaStore.MediaColumns.DATA + "=?",
				new String[] {file.getAbsolutePath()});
	}

	return !file.exists();
}
 
Example 6
Source File: OutputUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
public static @Nullable DocumentFile getWritingDocumentFileForAppItem(@NonNull Context context,@NonNull AppItem appItem,@NonNull String extension,int sequence_number) throws Exception{
    String writingFileName=getWriteFileNameForAppItem(context,appItem,extension,sequence_number);
    DocumentFile parent=getExportPathDocumentFile(context);
    DocumentFile documentFile=parent.findFile(writingFileName);
    if(documentFile!=null&&documentFile.exists())documentFile.delete();
    return parent.createFile(extension.toLowerCase().equals("apk")?"application/vnd.android.package-archive":"application/x-zip-compressed",writingFileName);
}
 
Example 7
Source File: FileUtils.java    From Rey-MusicPlayer with Apache License 2.0 5 votes vote down vote up
private static boolean deleteDirectory(DocumentFile documentFile) {
    DocumentFile files[] = documentFile.listFiles();
    for (DocumentFile file : files) {
        Logger.log("File Name :-  " + file.getName());
        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            return file.delete();
        }
    }
    return false;
}
 
Example 8
Source File: Delete.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
boolean deleteFileOnRemovableStorage(Context context, Uri treeUri, String path) {
    boolean success = false;
    DocumentFile file = StorageUtil.parseDocumentFile(context, treeUri, new File(path));
    if (file != null) {
        success = file.delete();
    }
    //remove from MediaStore
    addPathToScan(path);
    return success;
}
 
Example 9
Source File: FileUtil.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
private static boolean rmdir(final File file, Context context) {
    if (file == null)
        return false;
    if (!file.exists()) {
        return true;
    }
    if (!file.isDirectory()) {
        return false;
    }
    String[] fileList = file.list();
    if (fileList != null && fileList.length > 0) {
        //  empty the folder.
        rmdir1(file, context);
    }
    String[] fileList1 = file.list();
    if (fileList1 != null && fileList1.length > 0) {
        // Delete only empty folder.
        return false;
    }
    // Try the normal way
    if (file.delete()) {
        return true;
    }

    // Try with Storage Access Framework.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DocumentFile document = getDocumentFile(file, true, context);
        return document.delete();
    }

    // Try the Kitkat workaround.
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
        resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        // Delete the created entry, such that content provider will delete the file.
        resolver.delete(MediaStore.Files.getContentUri("external"), MediaStore.MediaColumns.DATA + "=?",
                new String[]{file.getAbsolutePath()});
    }

    return !file.exists();
}
 
Example 10
Source File: ZipStorageDocumentFile.java    From ToGoZip with GNU General Public License v3.0 4 votes vote down vote up
/**
 *  {@inheritDoc}
 */
@Override
public boolean delete(ZipInstance zipInstance) {
    DocumentFile zipFile = directory.findFile(getZipFileNameWithoutPath(zipInstance));
    return (zipFile != null) && zipFile.delete();
}
 
Example 11
Source File: DeleteFileUtils.java    From Gallery-example with GNU General Public License v3.0 2 votes vote down vote up
private static void deleteFileSAF(String url, final Activity activity) {

        if (SDCardUtils.getSDCardUri(activity).isEmpty()) {

            PermissionUtils.askSDCardAccess(activity);

        } else {

            File file = new File(url);

            DocumentFile documentFile = DocumentFile.fromTreeUri(activity, Uri.parse(SDCardUtils.getSDCardUri(activity)));

            String[] parts = (file.getPath()).split("\\/");

            for (int i = 3; i < parts.length; i++) {
                if (documentFile != null) {
                    documentFile = documentFile.findFile(parts[i]);
                }
            }

            if (documentFile == null) {

                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, activity.getString(R.string.notFound), Toast.LENGTH_SHORT)
                                .show();

                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

                        rationaleDialog(activity, activity.getString(R.string.sdcard), activity.getString(R.string.sdcardContent), 2, intent);
                    }
                });

            } else {

                if (documentFile.delete()) {

                    deleteFileFromMediaStore(activity.getContentResolver(), file);

                }
            }
        }
    }