Search in sources :

Example 51 with DocumentFile

use of android.support.v4.provider.DocumentFile in project LibreraReader by foobnix.

the class DefaultListeners method deleteFile.

@SuppressLint("NewApi")
private static void deleteFile(Activity a, final FileMetaAdapter searchAdapter, final FileMeta result) {
    boolean delete = false;
    if (ExtUtils.isExteralSD(result.getPath())) {
        DocumentFile doc = DocumentFile.fromSingleUri(a, Uri.parse(result.getPath()));
        delete = doc.delete();
    } else {
        final File file = new File(result.getPath());
        delete = file.delete();
    }
    LOG.d("Delete-file", result.getPath(), delete);
    if (delete) {
        TempHolder.listHash++;
        AppDB.get().delete(result);
        searchAdapter.getItemsList().remove(result);
        searchAdapter.notifyDataSetChanged();
    } else {
        Toast.makeText(a, R.string.can_t_delete_file, Toast.LENGTH_LONG).show();
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) SuppressLint(android.annotation.SuppressLint)

Example 52 with DocumentFile

use of android.support.v4.provider.DocumentFile in project AmazeFileManager by TeamAmaze.

the class GenericCopyUtil method startCopy.

/**
 * Starts copy of file
 * Supports : {@link File}, {@link jcifs.smb.SmbFile}, {@link DocumentFile}, {@link CloudStorage}
 * @param lowOnMemory defines whether system is running low on memory, in which case we'll switch to
 *                    using streams instead of channel which maps the who buffer in memory.
 *                    TODO: Use buffers even on low memory but don't map the whole file to memory but
 *                          parts of it, and transfer each part instead.
 * @throws IOException
 */
private void startCopy(boolean lowOnMemory) throws IOException {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    BufferedInputStream bufferedInputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
        // initializing the input channels based on file types
        if (mSourceFile.isOtgFile()) {
            // source is in otg
            ContentResolver contentResolver = mContext.getContentResolver();
            DocumentFile documentSourceFile = OTGUtil.getDocumentFile(mSourceFile.getPath(), mContext, false);
            bufferedInputStream = new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isSmb()) {
            // source is in smb
            bufferedInputStream = new BufferedInputStream(mSourceFile.getInputStream(), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isSftp()) {
            bufferedInputStream = new BufferedInputStream(mSourceFile.getInputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mSourceFile.isDropBoxFile()) {
            CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
            bufferedInputStream = new BufferedInputStream(cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, mSourceFile.getPath())));
        } else if (mSourceFile.isBoxFile()) {
            CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
            bufferedInputStream = new BufferedInputStream(cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, mSourceFile.getPath())));
        } else if (mSourceFile.isGoogleDriveFile()) {
            CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
            bufferedInputStream = new BufferedInputStream(cloudStorageGdrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, mSourceFile.getPath())));
        } else if (mSourceFile.isOneDriveFile()) {
            CloudStorage cloudStorageOnedrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
            bufferedInputStream = new BufferedInputStream(cloudStorageOnedrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, mSourceFile.getPath())));
        } else {
            // source file is neither smb nor otg; getting a channel from direct file instead of stream
            File file = new File(mSourceFile.getPath());
            if (FileUtil.isReadable(file)) {
                if (mTargetFile.isOneDriveFile() || mTargetFile.isDropBoxFile() || mTargetFile.isGoogleDriveFile() || mTargetFile.isBoxFile() || lowOnMemory) {
                    // our target is cloud, we need a stream not channel
                    bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                } else {
                    inChannel = new RandomAccessFile(file, "r").getChannel();
                }
            } else {
                ContentResolver contentResolver = mContext.getContentResolver();
                DocumentFile documentSourceFile = FileUtil.getDocumentFile(file, mSourceFile.isDirectory(), mContext);
                bufferedInputStream = new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()), DEFAULT_BUFFER_SIZE);
            }
        }
        // initializing the output channels based on file types
        if (mTargetFile.isOtgFile()) {
            // target in OTG, obtain streams from DocumentFile Uri's
            ContentResolver contentResolver = mContext.getContentResolver();
            DocumentFile documentTargetFile = OTGUtil.getDocumentFile(mTargetFile.getPath(), mContext, true);
            bufferedOutputStream = new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isSftp()) {
            bufferedOutputStream = new BufferedOutputStream(mTargetFile.getOutputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isSmb()) {
            bufferedOutputStream = new BufferedOutputStream(mTargetFile.getOutputStream(mContext), DEFAULT_BUFFER_SIZE);
        } else if (mTargetFile.isDropBoxFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
            if (mSourceFile.isDropBoxFile()) {
                // we're in the same provider, use api method
                cloudStorageDropbox.copy(CloudUtil.stripPath(OpenMode.DROPBOX, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.DROPBOX, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                return;
            }
        } else if (mTargetFile.isBoxFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
            if (mSourceFile.isBoxFile()) {
                // we're in the same provider, use api method
                cloudStorageBox.copy(CloudUtil.stripPath(OpenMode.BOX, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.BOX, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else if (mTargetFile.isGoogleDriveFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
            if (mSourceFile.isGoogleDriveFile()) {
                // we're in the same provider, use api method
                cloudStorageGdrive.copy(CloudUtil.stripPath(OpenMode.GDRIVE, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.GDRIVE, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else if (mTargetFile.isOneDriveFile()) {
            // API doesn't support output stream, we'll upload the file directly
            CloudStorage cloudStorageOnedrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
            if (mSourceFile.isOneDriveFile()) {
                // we're in the same provider, use api method
                cloudStorageOnedrive.copy(CloudUtil.stripPath(OpenMode.ONEDRIVE, mSourceFile.getPath()), CloudUtil.stripPath(OpenMode.ONEDRIVE, mTargetFile.getPath()));
                return;
            } else {
                cloudStorageOnedrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE, mTargetFile.getPath()), bufferedInputStream, mSourceFile.getSize(), true);
                bufferedInputStream.close();
                return;
            }
        } else {
            // copying normal file, target not in OTG
            File file = new File(mTargetFile.getPath());
            if (FileUtil.isWritable(file)) {
                if (lowOnMemory) {
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                } else {
                    outChannel = new RandomAccessFile(file, "rw").getChannel();
                }
            } else {
                ContentResolver contentResolver = mContext.getContentResolver();
                DocumentFile documentTargetFile = FileUtil.getDocumentFile(file, mTargetFile.isDirectory(), mContext);
                bufferedOutputStream = new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()), DEFAULT_BUFFER_SIZE);
            }
        }
        if (bufferedInputStream != null) {
            if (bufferedOutputStream != null)
                copyFile(bufferedInputStream, bufferedOutputStream);
            else if (outChannel != null) {
                copyFile(bufferedInputStream, outChannel);
            }
        } else if (inChannel != null) {
            if (bufferedOutputStream != null)
                copyFile(inChannel, bufferedOutputStream);
            else if (outChannel != null)
                copyFile(inChannel, outChannel);
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(getClass().getSimpleName(), "I/O Error!");
        throw new IOException();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        // we ran out of memory to map the whole channel, let's switch to streams
        AppConfig.toast(mContext, mContext.getResources().getString(R.string.copy_low_memory));
        startCopy(true);
    } finally {
        try {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
            if (bufferedInputStream != null)
                bufferedInputStream.close();
            if (bufferedOutputStream != null)
                bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        // failure in closing stream
        }
    }
}
Also used : CloudStorage(com.cloudrail.si.interfaces.CloudStorage) DocumentFile(android.support.v4.provider.DocumentFile) RandomAccessFile(java.io.RandomAccessFile) BufferedInputStream(java.io.BufferedInputStream) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) RandomAccessFile(java.io.RandomAccessFile) HybridFile(com.amaze.filemanager.filesystem.HybridFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) ContentResolver(android.content.ContentResolver)

Example 53 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class StorageUtil method parseDocumentFile.

public static DocumentFile parseDocumentFile(Context context, Uri treeUri, File file) {
    DocumentFile treeRoot;
    try {
        treeRoot = DocumentFile.fromTreeUri(context, treeUri);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return null;
    }
    String path;
    try {
        path = file.getCanonicalPath();
        String sdCardPath = getSdCardRootPath(context, path);
        if (sdCardPath != null) {
            if (sdCardPath.equals(path)) {
                return treeRoot;
            }
            path = path.substring(sdCardPath.length() + 1);
        } else {
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    Log.d("StorageUtil", "path: " + path);
    if (treeRoot != null) {
        treeRoot = DocumentFile.fromTreeUri(context, treeUri);
        String[] pathParts = path.split("/");
        DocumentFile documentFile = treeRoot;
        for (int i = 0; i < pathParts.length; i++) {
            if (documentFile != null) {
                documentFile = documentFile.findFile(pathParts[i]);
            } else {
                return null;
            }
        }
        return documentFile;
    }
    return null;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) IOException(java.io.IOException)

Example 54 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class StorageUtil method createDocumentFile.

public static DocumentFile createDocumentFile(Context context, Uri treeUri, String path, String mimeType) {
    int index = path.lastIndexOf("/");
    String dirPath = path.substring(0, index);
    DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath));
    if (file != null) {
        String name = path.substring(index + 1);
        file = file.createFile(mimeType, name);
    }
    return file;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 55 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class Copy method copyFileOntoRemovableStorage.

// for files on removable storage
static boolean copyFileOntoRemovableStorage(Context context, Uri treeUri, String path, String destination) throws IOException {
    String mimeType = MediaType.getMimeType(path);
    DocumentFile file = DocumentFile.fromFile(new File(destination));
    if (file.exists()) {
        int index = destination.lastIndexOf(".");
        destination = destination.substring(0, index) + " Copy" + destination.substring(index, destination.length());
    }
    DocumentFile destinationFile = StorageUtil.createDocumentFile(context, treeUri, destination, mimeType);
    if (destinationFile != null) {
        ContentResolver resolver = context.getContentResolver();
        OutputStream outputStream = resolver.openOutputStream(destinationFile.getUri());
        InputStream inputStream = new FileInputStream(path);
        return writeStream(inputStream, outputStream);
    }
    return false;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) FileInputStream(java.io.FileInputStream) ContentResolver(android.content.ContentResolver)

Aggregations

DocumentFile (android.support.v4.provider.DocumentFile)56 File (java.io.File)30 Uri (android.net.Uri)21 IOException (java.io.IOException)17 ContentResolver (android.content.ContentResolver)10 FileOutputStream (java.io.FileOutputStream)8 SuppressLint (android.annotation.SuppressLint)6 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 OutputStream (java.io.OutputStream)5 MalformedURLException (java.net.MalformedURLException)4 FileChannel (java.nio.channels.FileChannel)4 SmbException (jcifs.smb.SmbException)4 SmbFile (jcifs.smb.SmbFile)4 ContentValues (android.content.ContentValues)3 SharedPreferences (android.content.SharedPreferences)3 ParcelFileDescriptor (android.os.ParcelFileDescriptor)3 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)3 BufferedOutputStream (java.io.BufferedOutputStream)3 FileNotFoundException (java.io.FileNotFoundException)3