Example usage for android.os ParcelFileDescriptor open

List of usage examples for android.os ParcelFileDescriptor open

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor open.

Prototype

public static ParcelFileDescriptor open(File file, int mode, Handler handler, final OnCloseListener listener)
        throws IOException 

Source Link

Document

Create a new ParcelFileDescriptor accessing a given file.

Usage

From source file:com.seafile.seadroid2.provider.SeafileProvider.java

/**
 * Create ParcelFileDescriptor from the given file.
 *
 * @param file the file/*from  ww  w  . java  2 s .  c  om*/
 * @param mode the mode the file shoall be opened with.
 * @return a ParcelFileDescriptor
 * @throws IOException
 */
private ParcelFileDescriptor makeParcelFileDescriptor(final DataManager dm, final String repoName,
        final String repoID, final String parentDir, final File file, final String mode) throws IOException {
    final int accessMode = ParcelFileDescriptor.parseMode(mode);

    Handler handler = new Handler(getContext().getMainLooper());

    return ParcelFileDescriptor.open(file, accessMode, handler, new ParcelFileDescriptor.OnCloseListener() {
        @Override
        public void onClose(final IOException e) {
            Log.d(DEBUG_TAG,
                    "uploading file: " + repoID + "; " + file.getPath() + "; " + parentDir + "; e=" + e);

            if (mode.equals("r") || e != null) {
                return;
            }

            ConcurrentAsyncTask.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        dm.uploadFile(repoName, repoID, parentDir, file.getPath(), null, true, false);

                        // update cache for parent dir
                        dm.getDirentsFromServer(repoID, parentDir);
                    } catch (SeafException e1) {
                        Log.d(DEBUG_TAG, "could not upload file: ", e1);
                    }
                }
            });
        }

    });
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

private ParcelFileDescriptor openFileArtwork(@NonNull final Uri uri, @NonNull final String mode)
        throws FileNotFoundException {
    String[] projection = { BaseColumns._ID, MuzeiContract.Artwork.COLUMN_NAME_IMAGE_URI };
    final boolean isWriteOperation = mode.contains("w");
    final File file;
    if (!UserManagerCompat.isUserUnlocked(getContext())) {
        if (isWriteOperation) {
            Log.w(TAG, "Wallpaper is read only until the user is unlocked");
            return null;
        }/*from   w ww .  ja  va2  s  . co m*/
        file = DirectBootCacheJobService.getCachedArtwork(getContext());
    } else if (!isWriteOperation && MuzeiProvider.uriMatcher.match(uri) == MuzeiProvider.ARTWORK) {
        // If it isn't a write operation, then we should attempt to find the latest artwork
        // that does have a cached artwork file. This prevents race conditions where
        // an external app attempts to load the latest artwork while an art source is inserting a
        // new artwork
        Cursor data = queryArtwork(MuzeiContract.Artwork.CONTENT_URI, projection, null, null, null);
        if (data == null) {
            return null;
        }
        if (!data.moveToFirst()) {
            if (!getContext().getPackageName().equals(getCallingPackage())) {
                Log.w(TAG, "You must insert at least one row to read or write artwork");
            }
            return null;
        }
        File foundFile = null;
        while (!data.isAfterLast()) {
            Uri possibleArtworkUri = ContentUris.withAppendedId(MuzeiContract.Artwork.CONTENT_URI,
                    data.getLong(0));
            File possibleFile = getCacheFileForArtworkUri(possibleArtworkUri);
            if (possibleFile != null && possibleFile.exists()) {
                foundFile = possibleFile;
                break;
            }
            data.moveToNext();
        }
        file = foundFile;
    } else {
        file = getCacheFileForArtworkUri(uri);
    }
    if (file == null) {
        throw new FileNotFoundException("Could not create artwork file");
    }
    if (file.exists() && file.length() > 0 && isWriteOperation) {
        Context context = getContext();
        if (context == null) {
            return null;
        }
        if (!context.getPackageName().equals(getCallingPackage())) {
            Log.w(TAG, "Writing to an existing artwork file is not allowed: insert a new row");
        }
        cleanupCachedFiles();
        return null;
    }
    try {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode), openFileHandler,
                new ParcelFileDescriptor.OnCloseListener() {
                    @Override
                    public void onClose(final IOException e) {
                        if (isWriteOperation) {
                            if (e != null) {
                                Log.e(TAG, "Error closing " + file + " for " + uri, e);
                                if (file.exists()) {
                                    if (!file.delete()) {
                                        Log.w(TAG, "Unable to delete " + file);
                                    }
                                }
                            } else {
                                // The file was successfully written, notify listeners of the new artwork
                                notifyChange(uri);
                                cleanupCachedFiles();
                            }
                        }

                    }
                });
    } catch (IOException e) {
        Log.e(TAG, "Error opening artwork " + uri, e);
        throw new FileNotFoundException("Error opening artwork " + uri);
    }
}

From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java

private ParcelFileDescriptor createFileDescriptor(
        final org.alfresco.mobile.android.api.model.Document currentNode, boolean isWrite, final File file,
        int accessMode) throws FileNotFoundException {
    if (isWrite) {
        // Attach a close listener if the document is opened in write mode.
        try {/*w w  w.  j a  v a  2  s .  c o m*/
            Handler handler = new Handler(getContext().getMainLooper());
            return ParcelFileDescriptor.open(file, accessMode, handler,
                    new ParcelFileDescriptor.OnCloseListener() {
                        @Override
                        public void onClose(IOException e) {
                            Operator.with(getContext(), selectedAccount)
                                    .load(new UpdateContentRequest.Builder(parentFolder, currentNode,
                                            new ContentFileProgressImpl(file, currentNode.getName(),
                                                    currentNode.getContentStreamMimeType())));
                        }

                    });
        } catch (IOException e) {
            throw new FileNotFoundException("Failed to open document");
        }
    } else {
        return ParcelFileDescriptor.open(file, accessMode);
    }
}