List of usage examples for android.support.v4.provider DocumentFile delete
public abstract boolean delete();
From source file:com.osama.cryptofm.tasks.DeleteTask.java
private void deleteDocFile(DocumentFile docFile) throws IOException { Log.d(TAG, "deleteDocFile: Deleting document file"); publishProgress(docFile.getName());// w ww .j av a 2 s .c om if (docFile.isDirectory() && docFile.listFiles().length > 0) { for (DocumentFile d : docFile.listFiles()) { deleteDocFile(d); } } if (!docFile.delete()) { throw new IOException("cannot delete files"); } }
From source file:com.xperia64.timidityae.Globals.java
public static void tryToDeleteFile(Context c, String filename) { filename = filename.replace("//", "/"); if (new File(filename).exists()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Globals.theFold != null) { DocumentFile df = DocumentFile.fromTreeUri(c, theFold); String split[] = filename.split("/"); int i; for (i = 0; i < split.length; i++) { if (split[i].equals(df.getName())) { i++;/* w w w .j av a2s. co m*/ break; } } DocumentFile xx = df; while (i < split.length) { xx = xx.findFile(split[i++]); //upper.append("../"); if (xx == null) { Log.e("TimidityAE Globals", "Delete file error."); break; } } // Why on earth is DocumentFile's delete method recursive by default? // Seriously. I wiped my sd card twice because of this. if (xx != null && xx.isFile() && !xx.isDirectory()) { xx.delete(); } } else { new File(filename).delete(); } } }
From source file:com.frostwire.android.LollipopFileSystem.java
@Override public boolean delete(File file) { if (file.delete()) { return true; }//w ww . jav a 2s .co m DocumentFile f = getDocument(app, file); return f != null && f.delete(); }
From source file:freed.ActivityAbstract.java
@Nullable private boolean deletFile(File file) { if (!file.delete()) { DocumentFile sdDir = getExternalSdDocumentFile(); if (sdDir == null) return false; String baseS = sdDir.getName(); String fileFolder = file.getAbsolutePath(); String[] split = fileFolder.split("/"); DocumentFile tmpdir = null; boolean append = false; for (String aSplit : split) { if (aSplit.equals(baseS) || append) { if (!append) { append = true;// w w w .ja v a 2 s. com tmpdir = sdDir; } else { tmpdir = tmpdir.findFile(aSplit); } } } boolean d = false; d = !(tmpdir != null && tmpdir.exists()) || tmpdir.delete(); Log.d("delteDocumentFile", "file delted:" + d); return d; } return true; }
From source file:de.arcus.playmusiclib.PlayMusicManager.java
/** * Exports a track to the sd card//from w ww. ja v a 2 s. co m * @param musicTrack The music track you want to export * @param uri The document tree * @return Returns whether the export was successful */ public boolean exportMusicTrack(MusicTrack musicTrack, Uri uri, String path) { // Check for null if (musicTrack == null) return false; String srcFile = musicTrack.getSourceFile(); // Could not find the source file if (srcFile == null) return false; String fileTmp = getTempPath() + "/tmp.mp3"; // Copy to temp path failed if (!SuperUserTools.fileCopy(srcFile, fileTmp)) return false; // Encrypt the file if (musicTrack.isEncoded()) { String fileTmpCrypt = getTempPath() + "/crypt.mp3"; // Encrypts the file if (trackEncrypt(musicTrack, fileTmp, fileTmpCrypt)) { // Remove the old tmp file FileTools.fileDelete(fileTmp); // New tmp file fileTmp = fileTmpCrypt; } else { Logger.getInstance().logWarning("ExportMusicTrack", "Encrypting failed! Continue with decrypted file."); } } String dest; Uri copyUri = null; if (uri.toString().startsWith("file://")) { // Build the full path dest = uri.buildUpon().appendPath(path).build().getPath(); String parentDirectory = new File(dest).getParent(); FileTools.directoryCreate(parentDirectory); } else { // Complex uri (Lollipop) dest = getTempPath() + "/final.mp3"; // The root DocumentFile document = DocumentFile.fromTreeUri(mContext, uri); // Creates the subdirectories String[] directories = path.split("\\/"); for (int i = 0; i < directories.length - 1; i++) { String directoryName = directories[i]; boolean found = false; // Search all sub elements for (DocumentFile subDocument : document.listFiles()) { // Directory exists if (subDocument.isDirectory() && subDocument.getName().equals(directoryName)) { document = subDocument; found = true; break; } } if (!found) { // Create the directory document = document.createDirectory(directoryName); } } // Gets the filename String filename = directories[directories.length - 1]; for (DocumentFile subDocument : document.listFiles()) { // Directory exists if (subDocument.isFile() && subDocument.getName().equals(filename)) { // Delete the file subDocument.delete(); break; } } // Create the mp3 file document = document.createFile("music/mp3", filename); // Create the directories copyUri = document.getUri(); } // We want to export the ID3 tags if (mID3Enable) { // Adds the meta data if (!trackWriteID3(musicTrack, fileTmp, dest)) { Logger.getInstance().logWarning("ExportMusicTrack", "ID3 writer failed! Continue without ID3 tags."); // Failed, moving without meta data if (!FileTools.fileMove(fileTmp, dest)) { Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!"); // Could not copy the file return false; } } } else { // Moving the file if (!FileTools.fileMove(fileTmp, dest)) { Logger.getInstance().logError("ExportMusicTrack", "Moving the raw file failed!"); // Could not copy the file return false; } } // We need to copy the file to a uri if (copyUri != null) { // Lollipop only if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { // Gets the file descriptor ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver() .openFileDescriptor(copyUri, "w"); // Gets the output stream FileOutputStream fileOutputStream = new FileOutputStream( parcelFileDescriptor.getFileDescriptor()); // Gets the input stream FileInputStream fileInputStream = new FileInputStream(dest); // Copy the stream FileTools.fileCopy(fileInputStream, fileOutputStream); // Close all streams fileOutputStream.close(); fileInputStream.close(); parcelFileDescriptor.close(); } catch (FileNotFoundException e) { Logger.getInstance().logError("ExportMusicTrack", "File not found!"); // Could not copy the file return false; } catch (IOException e) { Logger.getInstance().logError("ExportMusicTrack", "Failed to write the document: " + e.toString()); // Could not copy the file return false; } } } // Delete temp files cleanUp(); // Adds the file to the media system //new MediaScanner(mContext, dest); // Done return true; }
From source file:com.osama.cryptofm.tasks.MoveTask.java
private void moveDocumentFile(DocumentFile file) throws Exception { if (file.isDirectory()) { Log.d(TAG, "moveDocumentFile: Yes document file is directory"); //change the destination folder mDestinationFolder = mDestinationFolder + file.getName() + "/"; //check if destination folder is not document file if (FileUtils.isDocumentFile(mDestinationFolder)) { rootDocumentFile = rootDocumentFile.createDirectory(file.getName()); } else {/*w w w . j av a 2 s . co m*/ File tmp = new File(mDestinationFolder); if (!tmp.exists()) { tmp.mkdir(); } else { return; } } for (DocumentFile f : file.listFiles()) { moveDocumentFile(f); } } else { isNextFile = true; publishProgress(file.getName()); publishProgress("" + 0); //check if pasting in internal storage if (!FileUtils.isDocumentFile(mDestinationFolder)) { Log.d(TAG, "moveDocumentFile: moving document file in internal storage"); File destinationFile = new File(mDestinationFolder + file.getName()); innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()), new BufferedOutputStream(new FileOutputStream(destinationFile)), file.length()); } else { Log.d(TAG, "moveDocumentFile: Moving document file honey"); DocumentFile destFile = rootDocumentFile.createFile(file.getType(), file.getName()); innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()), CryptoFM.getContext().getContentResolver().openOutputStream(destFile.getUri()), file.length()); } } //delete the input file //if copying then don't if (SharedData.IS_COPYING_NOT_MOVING) { return; } if (!file.delete()) { throw new IOException("cannot move files"); } }
From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java
protected void doExportVideo() { boolean onPause = this.onPause; this.onPause = false; boolean isDro = this.modeDRO(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (documentFileSaved != null || !isDro)) { DocumentFile fileSaved = VideoCapturePlugin.documentFileSaved; ArrayList<DocumentFile> filesListToExport = documentFilesList; String resultName = fileSaved.getName(); DocumentFile resultFile = fileSaved; if (filesListToExport.size() > 0) { int inputFileCount = filesListToExport.size(); if (!onPause) inputFileCount++;//from w w w . ja v a 2s . c om DocumentFile[] inputFiles = new DocumentFile[inputFileCount]; for (int i = 0; i < filesListToExport.size(); i++) { inputFiles[i] = filesListToExport.get(i); } // If video recording hadn't been paused before STOP was // pressed, then last recorded file is not in the list with // other files, added to list of files manually. if (!onPause) { inputFiles[inputFileCount - 1] = fileSaved; } resultFile = appendNew(inputFiles); // Remove merged files, except first one, because it stores the // result of merge. for (int i = 0; i < filesListToExport.size(); i++) { DocumentFile currentFile = filesListToExport.get(i); currentFile.delete(); } // If video recording hadn't been paused before STOP was // pressed, then last recorded file is not in the list with // other files, and should be deleted manually. if (!onPause) fileSaved.delete(); String tmpName = resultFile.getName(); if (resultFile.renameTo(resultName)) ; // Make sure, that there won't be duplicate broken file // in phone memory at gallery. String args[] = { tmpName }; ApplicationScreen.instance.getContentResolver().delete(Video.Media.EXTERNAL_CONTENT_URI, Video.Media.DISPLAY_NAME + "=?", args); } String name = resultFile.getName(); String data = null; // If we able to get File object, than get path from it. Gallery // doesn't show the file, if it's stored at phone memory and // we need insert new file to gallery manually. File file = Util.getFileFromDocumentFile(resultFile); if (file != null) { data = file.getAbsolutePath(); } else { // This case should typically happen for files saved to SD // card. data = Util.getAbsolutePathFromDocumentFile(resultFile); } if (data != null) { values.put(VideoColumns.DISPLAY_NAME, name); values.put(VideoColumns.DATA, data); Uri uri = ApplicationScreen.instance.getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, values); ApplicationScreen.getMainContext().sendBroadcast(new Intent(ACTION_NEW_VIDEO, uri)); } } else { File fileSaved = VideoCapturePlugin.fileSaved; ArrayList<File> filesListToExport = filesList; File firstFile = fileSaved; if (filesListToExport.size() > 0) { firstFile = filesListToExport.get(0); int inputFileCount = filesListToExport.size(); if (!onPause) inputFileCount++; File[] inputFiles = new File[inputFileCount]; for (int i = 0; i < filesListToExport.size(); i++) { inputFiles[i] = filesListToExport.get(i); } if (!onPause) inputFiles[inputFileCount - 1] = fileSaved; File resultFile = append(inputFiles); for (int i = 0; i < filesListToExport.size(); i++) { File currentFile = filesListToExport.get(i); currentFile.delete(); } if (resultFile != null) { if (!resultFile.getAbsoluteFile().equals(fileSaved.getAbsoluteFile())) { fileSaved.delete(); resultFile.renameTo(fileSaved); } } } filesListToExport.clear(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isDro) { DocumentFile outputFile = getOutputMediaDocumentFile(); File file = Util.getFileFromDocumentFile(outputFile); if (file != null) { // Don't do anything with ouputFile. It's useless, remove // it. outputFile.delete(); Uri uri = ApplicationScreen.instance.getContentResolver() .insert(Video.Media.EXTERNAL_CONTENT_URI, values); ApplicationScreen.getMainContext().sendBroadcast(new Intent(ACTION_NEW_VIDEO, uri)); } else { // Copy result file from phone memory to selected folder at // SD-card. InputStream is = null; int len; byte[] buf = new byte[4096]; try { OutputStream os = ApplicationScreen.instance.getContentResolver() .openOutputStream(outputFile.getUri()); is = new FileInputStream(firstFile); while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } is.close(); os.close(); firstFile.delete(); // Make sure, that there won't be duplicate broken file // in phone memory at gallery. String args[] = { firstFile.getAbsolutePath() }; ApplicationScreen.instance.getContentResolver().delete(Video.Media.EXTERNAL_CONTENT_URI, Video.Media.DATA + "=?", args); String data = Util.getAbsolutePathFromDocumentFile(outputFile); if (data != null) { values.put(VideoColumns.DATA, data); Uri uri = ApplicationScreen.instance.getContentResolver() .insert(Video.Media.EXTERNAL_CONTENT_URI, values); ApplicationScreen.getMainContext().sendBroadcast(new Intent(ACTION_NEW_VIDEO, uri)); } } catch (Exception e) { e.printStackTrace(); } } } else { Uri uri = ApplicationScreen.instance.getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, values); ApplicationScreen.getMainContext().sendBroadcast(new Intent(ACTION_NEW_VIDEO, uri)); } } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } ApplicationScreen.getMessageHandler().sendEmptyMessage(ApplicationInterface.MSG_EXPORT_FINISHED); }