Example usage for android.os ParcelFileDescriptor MODE_READ_ONLY

List of usage examples for android.os ParcelFileDescriptor MODE_READ_ONLY

Introduction

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

Prototype

int MODE_READ_ONLY

To view the source code for android.os ParcelFileDescriptor MODE_READ_ONLY.

Click Source Link

Document

For use with #open : open the file with read-only access.

Usage

From source file:com.facebook.Request.java

/**
 * Creates a new Request configured to upload a photo to the user's default photo album. The photo
 * will be read from the specified stream.
 *
 * @param session  the Session to use, or null; if non-null, the session must be in an opened state
 * @param file     the file containing the photo to upload
 * @param callback a callback that will be called when the request is completed to handle success or error conditions
 * @return a Request that is ready to execute
 *//*from  w ww.  j  a  v  a2 s  . co  m*/
public static Request newUploadPhotoRequest(Session session, File file, Callback callback)
        throws FileNotFoundException {
    ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle parameters = new Bundle(1);
    parameters.putParcelable(PICTURE_PARAM, descriptor);

    return new Request(session, MY_PHOTOS, parameters, HttpMethod.POST, callback);
}

From source file:com.facebook.Request.java

/**
 * Creates a new Request configured to upload a photo to the user's default photo album. The photo
 * will be read from the specified file descriptor.
 *
 * @param session  the Session to use, or null; if non-null, the session must be in an opened state
 * @param file     the file to upload//w w w . ja v  a2s  .  c  om
 * @param callback a callback that will be called when the request is completed to handle success or error conditions
 * @return a Request that is ready to execute
 */
public static Request newUploadVideoRequest(Session session, File file, Callback callback)
        throws FileNotFoundException {
    ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle parameters = new Bundle(1);
    parameters.putParcelable(file.getName(), descriptor);

    return new Request(session, MY_VIDEOS, parameters, HttpMethod.POST, callback);
}

From source file:org.getlantern.firetweet.provider.FiretweetDataProvider.java

@Override
public ParcelFileDescriptor openFile(final Uri uri, final String mode) throws FileNotFoundException {
    if (uri == null || mode == null)
        throw new IllegalArgumentException();
    final int table_id = getTableId(uri);
    final String table = getTableNameById(table_id);
    final int mode_code;
    if ("r".equals(mode)) {
        mode_code = ParcelFileDescriptor.MODE_READ_ONLY;
    } else if ("rw".equals(mode)) {
        mode_code = ParcelFileDescriptor.MODE_READ_WRITE;
    } else if ("rwt".equals(mode)) {
        mode_code = ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_TRUNCATE;
    } else//from www.  ja  v a2  s.c  o  m
        throw new IllegalArgumentException();
    if (mode_code == ParcelFileDescriptor.MODE_READ_ONLY) {
        checkReadPermission(table_id, table, null);
    } else if ((mode_code & ParcelFileDescriptor.MODE_READ_WRITE) != 0) {
        checkReadPermission(table_id, table, null);
        checkWritePermission(table_id, table);
    }
    switch (table_id) {
    case VIRTUAL_TABLE_ID_CACHED_IMAGES: {
        return getCachedImageFd(uri.getQueryParameter(QUERY_PARAM_URL));
    }
    case VIRTUAL_TABLE_ID_CACHE_FILES: {
        return getCacheFileFd(uri.getLastPathSegment());
    }
    }
    return null;
}

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

@Override
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal)
        throws FileNotFoundException {
    // Log.v(TAG, "openDocumentThumbnail");
    try {/*from w  w  w  .ja va 2 s .co  m*/
        Node currentNode = null;
        EncodedQueryUri cUri = new EncodedQueryUri(documentId);
        if (cUri.type != PREFIX_DOC) {
            return null;
        }
        checkSession(cUri);

        currentNode = retrieveNode(cUri.id);

        // Let's retrieve the thumbnail
        // Store the document inside a temporary folder per account
        File downloadedFile = null;
        if (getContext() != null && currentNode != null && session != null) {
            File folder = AlfrescoStorageManager.getInstance(getContext()).getTempFolder(selectedAccount);
            if (folder != null) {
                downloadedFile = new File(folder, currentNode.getName());
            }
        } else {
            return null;
        }

        // Is Document in cache ?
        if (downloadedFile.exists()
                && currentNode.getModifiedAt().getTimeInMillis() < downloadedFile.lastModified()) {
            // Document available locally
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(downloadedFile,
                    ParcelFileDescriptor.MODE_READ_ONLY);
            return new AssetFileDescriptor(pfd, 0, downloadedFile.length());
        }

        // Not in cache so let's download the content !
        ContentStream contentStream = session.getServiceRegistry().getDocumentFolderService()
                .getRenditionStream(currentNode, DocumentFolderService.RENDITION_THUMBNAIL);

        // Check ContentStream
        if (contentStream == null || contentStream.getLength() == 0) {
            return null;
        }

        // Store the thumbnail locally
        copyFile(contentStream.getInputStream(), contentStream.getLength(), downloadedFile, signal);

        // Return the fileDescriptor
        if (downloadedFile.exists()) {
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(downloadedFile,
                    ParcelFileDescriptor.MODE_READ_ONLY);
            return new AssetFileDescriptor(pfd, 0, downloadedFile.length());
        } else {
            return null;
        }
    } catch (Exception e) {
        Log.w(TAG, Log.getStackTraceString(e));
        return null;
    }
}

From source file:net.sf.xfd.provider.PublicProvider.java

@Override
public Uri canonicalize(@NonNull Uri uri) {
    try {/*from   w  w w .j  a va  2 s  .  c  o m*/
        base.assertAbsolute(uri);

        String grantMode = uri.getQueryParameter(URI_ARG_MODE);
        if (TextUtils.isEmpty(grantMode)) {
            grantMode = "r";
        }

        verifyMac(uri, grantMode, grantMode);

        final int flags = ParcelFileDescriptor.parseMode(grantMode);

        final Context context = getContext();
        assert context != null;

        final String packageName = context.getPackageName();

        final Uri canon = DocumentsContract.buildDocumentUri(packageName + FileProvider.AUTHORITY_SUFFIX,
                canonString(uri.getPath()));

        final int callerUid = Binder.getCallingUid();

        if (callerUid != Process.myUid()) {
            int grantFlags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;

            if ((flags & ParcelFileDescriptor.MODE_READ_ONLY) == flags) {
                grantFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
            } else if ((flags & ParcelFileDescriptor.MODE_WRITE_ONLY) == flags)
                grantFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            else {
                grantFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
                grantFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
            }

            final String[] packages;

            final String caller = getCallingPackage();
            if (caller != null) {
                packages = new String[] { caller };
            } else {
                final PackageManager pm = context.getPackageManager();
                packages = pm.getPackagesForUid(callerUid);
            }

            if (packages != null) {
                for (String pkg : packages) {
                    context.grantUriPermission(pkg, canon, grantFlags);
                }
            }
        }

        return canon;
    } catch (FileNotFoundException e) {
        return null;
    }
}

From source file:dev.dworks.apps.anexplorer.provider.ExternalStorageProvider.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override//w w w.ja  v a2s  .c  om
public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal signal)
        throws FileNotFoundException {
    final File file = getFileForDocId(documentId);
    if (Utils.hasKitKat()) {
        final int pfdMode = ParcelFileDescriptor.parseMode(mode);
        if (pfdMode == ParcelFileDescriptor.MODE_READ_ONLY) {
            return ParcelFileDescriptor.open(file, pfdMode);
        } else {
            try {
                // When finished writing, kick off media scanner
                return ParcelFileDescriptor.open(file, pfdMode, mHandler,
                        new ParcelFileDescriptor.OnCloseListener() {
                            @Override
                            public void onClose(IOException e) {
                                FileUtils.updateMedia(getContext(), file.getPath());
                            }
                        });
            } catch (IOException e) {
                throw new FileNotFoundException("Failed to open for writing: " + e);
            }
        }
    } else {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);//ParcelFileDescriptor.parseMode(mode));
    }
}

From source file:de.vanita5.twittnuker.provider.TwidereDataProvider.java

private ParcelFileDescriptor getCachedImageFd(final String url) throws FileNotFoundException {
    if (Utils.isDebugBuild()) {
        Log.d(LOGTAG, String.format("getCachedImageFd(%s)", url));
    }//from  w  ww.ja  v a 2  s. c  o m
    final File file = mImagePreloader.getCachedImageFile(url);
    if (file == null)
        return null;
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}

From source file:de.vanita5.twittnuker.provider.TwidereDataProvider.java

private ParcelFileDescriptor getCacheFileFd(final String name) throws FileNotFoundException {
    if (name == null)
        return null;
    final Context mContext = getContext();
    final File cacheDir = mContext.getCacheDir();
    final File file = new File(cacheDir, name);
    if (!file.exists())
        return null;
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}

From source file:com.facebook.share.internal.ShareInternalUtility.java

/**
 * Creates a new Request configured to upload a photo to the specified graph path. The
 * photo will be read from the specified file.
 *
 * @param graphPath   the graph path to use
 * @param accessToken the access token to use, or null
 * @param file        the file containing the photo to upload
 * @param caption     the user generated caption for the photo.
 * @param params      the parameters//from www  .j  av  a 2s.co m
 * @param callback    a callback that will be called when the request is completed to handle
 *                    success or error conditions
 * @return a Request that is ready to execute
 * @throws java.io.FileNotFoundException
 */
public static GraphRequest newUploadPhotoRequest(String graphPath, AccessToken accessToken, File file,
        String caption, Bundle params, Callback callback) throws FileNotFoundException {
    ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle parameters = new Bundle();
    if (params != null) {
        parameters.putAll(params);
    }
    parameters.putParcelable(PICTURE_PARAM, descriptor);
    if (caption != null && !caption.isEmpty()) {
        parameters.putString(CAPTION_PARAM, caption);
    }

    return new GraphRequest(accessToken, graphPath, parameters, HttpMethod.POST, callback);
}