Java Code Examples for android.os.ParcelFileDescriptor#open()

The following examples show how to use android.os.ParcelFileDescriptor#open() . 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: NativeAppCallContentProvider.java    From Abelana-Android with Apache License 2.0 6 votes vote down vote up
@Override
public android.os.ParcelFileDescriptor openFile(android.net.Uri uri, java.lang.String mode)
        throws java.io.FileNotFoundException {

    Pair<UUID, String> callIdAndAttachmentName = parseCallIdAndAttachmentName(uri);
    if (callIdAndAttachmentName == null) {
        throw new FileNotFoundException();
    }

    try {
        File file = dataSource.openAttachment(callIdAndAttachmentName.first, callIdAndAttachmentName.second);

        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    } catch (FileNotFoundException exception) {
        Log.e(TAG, "Got unexpected exception:" + exception);
        throw exception;
    }
}
 
Example 2
Source File: StorageProvider.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
protected AssetFileDescriptor openVideoThumbnailCleared(long id, CancellationSignal signal)
        throws FileNotFoundException {
    final ContentResolver resolver = getContext().getContentResolver();
    Cursor cursor = null;
    try {
        cursor = resolver.query(Video.Thumbnails.EXTERNAL_CONTENT_URI,
                VideoThumbnailQuery.PROJECTION, Video.Thumbnails.VIDEO_ID + "=" + id, null,
                null);
        if (cursor.moveToFirst()) {
            final String data = cursor.getString(VideoThumbnailQuery._DATA);
            return new AssetFileDescriptor(ParcelFileDescriptor.open(
                    new File(data), ParcelFileDescriptor.MODE_READ_ONLY), 0,
                    AssetFileDescriptor.UNKNOWN_LENGTH);
        }
    } finally {
        IoUtils.closeQuietly(cursor);
    }
    return null;
}
 
Example 3
Source File: NativeAppCallContentProvider.java    From KlyphMessenger with MIT License 6 votes vote down vote up
@Override
public android.os.ParcelFileDescriptor openFile(android.net.Uri uri, java.lang.String mode)
        throws java.io.FileNotFoundException {

    Pair<UUID, String> callIdAndAttachmentName = parseCallIdAndAttachmentName(uri);
    if (callIdAndAttachmentName == null) {
        throw new FileNotFoundException();
    }

    try {
        File file = dataSource.openAttachment(callIdAndAttachmentName.first, callIdAndAttachmentName.second);

        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    } catch (FileNotFoundException exception) {
        Log.e(TAG, "Got unexpected exception:" + exception);
        throw exception;
    }
}
 
Example 4
Source File: ScienceJournalDocsProvider.java    From science-journal with Apache License 2.0 6 votes vote down vote up
@Override
public ParcelFileDescriptor openDocument(
    final String documentId, final String mode, CancellationSignal signal)
    throws FileNotFoundException {
  Log.v(TAG, "openDocument, mode: " + mode);

  AppAccount appAccount = getAppAccountFromDocumentId(documentId);

  final File file =
      new File(
          FileMetadataUtil.getInstance().getExperimentsRootDirectory(appAccount)
              + "/"
              + documentId);
  final boolean isWrite = (mode.indexOf('w') != -1);
  if (isWrite) {
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
  } else {
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
  }
}
 
Example 5
Source File: FileProvider.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    // ContentProvider has already checked granted permissions
    final File file = mStrategy.getFileForUri(uri);
    final int fileMode = modeToMode(mode);
    return ParcelFileDescriptor.open(file, fileMode);
}
 
Example 6
Source File: PdfRendererBasicViewModel.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
@WorkerThread
private void openPdfRenderer() throws IOException {
    final File file = new File(getApplication().getCacheDir(), FILENAME);
    if (!file.exists()) {
        // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
        // the cache directory.
        final InputStream asset = getApplication().getAssets().open(FILENAME);
        final FileOutputStream output = new FileOutputStream(file);
        final byte[] buffer = new byte[1024];
        int size;
        while ((size = asset.read(buffer)) != -1) {
            output.write(buffer, 0, size);
        }
        asset.close();
        output.close();
    }
    mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    if (mFileDescriptor != null) {
        mPdfRenderer = new PdfRenderer(mFileDescriptor);
    }
}
 
Example 7
Source File: FacebookContentProvider.java    From letv with Apache License 2.0 5 votes vote down vote up
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    Pair<UUID, String> callIdAndAttachmentName = parseCallIdAndAttachmentName(uri);
    if (callIdAndAttachmentName == null) {
        throw new FileNotFoundException();
    }
    try {
        return ParcelFileDescriptor.open(NativeAppCallAttachmentStore.openAttachment((UUID) callIdAndAttachmentName.first, (String) callIdAndAttachmentName.second), 268435456);
    } catch (FileNotFoundException exception) {
        Log.e(TAG, "Got unexpected exception:" + exception);
        throw exception;
    }
}
 
Example 8
Source File: LocalStorageProvider.java    From qiniu-lab-android with MIT License 5 votes vote down vote up
@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode,
                                         final CancellationSignal signal) throws FileNotFoundException {
    File file = new File(documentId);
    final boolean isWrite = (mode.indexOf('w') != -1);
    if (isWrite) {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
    } else {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    }
}
 
Example 9
Source File: LocalStorageProvider.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode,
                                         final CancellationSignal signal) throws FileNotFoundException {
    File file = new File(documentId);
    final boolean isWrite = (mode.indexOf('w') != -1);
    if (isWrite) {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
    } else {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    }
}
 
Example 10
Source File: RootedStorageProvider.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@Override
public ParcelFileDescriptor openDocument(
        String documentId, String mode, CancellationSignal signal)
        throws FileNotFoundException {
    final RootFile file = getRootFileForDocId(documentId);
    InputStream is = RootCommands.getFile(file.getPath());

    try {
        return ParcelFileDescriptorUtil.pipeFrom(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ParcelFileDescriptor.open(new File(file.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
}
 
Example 11
Source File: BarcodeProvider.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal) throws FileNotFoundException {
    Log.d(Config.LOGTAG, "opening file with uri (normal): " + uri.toString());
    String path = uri.getPath();
    if (path != null && path.endsWith(".png") && path.length() >= 5) {
        String jid = path.substring(1).substring(0, path.length() - 4);
        Log.d(Config.LOGTAG, "account:" + jid);
        if (connectAndWait()) {
            Log.d(Config.LOGTAG, "connected to background service");
            try {
                Account account = mXmppConnectionService.findAccountByJid(Jid.of(jid));
                if (account != null) {
                    String shareableUri = account.getShareableUri();
                    String hash = CryptoHelper.getFingerprint(shareableUri);
                    File file = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/" + hash);
                    if (!file.exists()) {
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                        Bitmap bitmap = create2dBarcodeBitmap(account.getShareableUri(), 1024);
                        OutputStream outputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                        outputStream.close();
                        outputStream.flush();
                    }
                    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                }
            } catch (Exception e) {
                throw new FileNotFoundException();
            }
        }
    }
    throw new FileNotFoundException();
}
 
Example 12
Source File: ReportFilesProvider.java    From android-crash-reporting with MIT License 5 votes vote down vote up
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException
{
	int fileIndex = sUriMatcher.match(uri);
	if(fileIndex != -1 && sPaths[fileIndex] != null){
		return ParcelFileDescriptor.open(new File(sPaths[fileIndex]), 
			ParcelFileDescriptor.MODE_READ_ONLY);
	}
	return super.openFile(uri, mode);
}
 
Example 13
Source File: UriImage.java    From droidddle with Apache License 2.0 5 votes vote down vote up
private ParcelFileDescriptor getPFD() {
    try {
        if (mUri.getScheme().equals("file")) {
            String path = mUri.getPath();
            return ParcelFileDescriptor.open(new File(path), ParcelFileDescriptor.MODE_READ_ONLY);
        } else {
            return mContentResolver.openFileDescriptor(mUri, "r");
        }
    } catch (FileNotFoundException ex) {
        return null;
    }
}
 
Example 14
Source File: StickerProvider.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode)
        throws FileNotFoundException {
    final File file = uriToFile(uri);
    if (!isFileInRoot(file)) {
        throw new SecurityException("File is not in root: " + file);
    }
    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}
 
Example 15
Source File: TvInputManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public ParcelFileDescriptor openDvbDevice(DvbDeviceInfo info, int device)
        throws RemoteException {
    if (mContext.checkCallingPermission(android.Manifest.permission.DVB_DEVICE)
            != PackageManager.PERMISSION_GRANTED) {
        throw new SecurityException("Requires DVB_DEVICE permission");
    }

    File devDirectory = new File("/dev");
    boolean dvbDeviceFound = false;
    for (String fileName : devDirectory.list()) {
        if (TextUtils.equals("dvb", fileName)) {
            File dvbDirectory = new File(DVB_DIRECTORY);
            for (String fileNameInDvb : dvbDirectory.list()) {
                Matcher adapterMatcher = sAdapterDirPattern.matcher(fileNameInDvb);
                if (adapterMatcher.find()) {
                    File adapterDirectory = new File(DVB_DIRECTORY + "/" + fileNameInDvb);
                    for (String fileNameInAdapter : adapterDirectory.list()) {
                        Matcher frontendMatcher = sFrontEndInAdapterDirPattern.matcher(
                                fileNameInAdapter);
                        if (frontendMatcher.find()) {
                            dvbDeviceFound = true;
                            break;
                        }
                    }
                }
                if (dvbDeviceFound) {
                    break;
                }
            }
        }
        if (dvbDeviceFound) {
            break;
        }
    }

    final long identity = Binder.clearCallingIdentity();
    try {
        String deviceFileName;
        switch (device) {
            case TvInputManager.DVB_DEVICE_DEMUX:
                deviceFileName = String.format(dvbDeviceFound
                        ? "/dev/dvb/adapter%d/demux%d" : "/dev/dvb%d.demux%d",
                        info.getAdapterId(), info.getDeviceId());
                break;
            case TvInputManager.DVB_DEVICE_DVR:
                deviceFileName = String.format(dvbDeviceFound
                        ? "/dev/dvb/adapter%d/dvr%d" : "/dev/dvb%d.dvr%d",
                        info.getAdapterId(), info.getDeviceId());
                break;
            case TvInputManager.DVB_DEVICE_FRONTEND:
                deviceFileName = String.format(dvbDeviceFound
                        ? "/dev/dvb/adapter%d/frontend%d" : "/dev/dvb%d.frontend%d",
                        info.getAdapterId(), info.getDeviceId());
                break;
            default:
                throw new IllegalArgumentException("Invalid DVB device: " + device);
        }
        try {
            // The DVB frontend device only needs to be opened in read/write mode, which
            // allows performing tuning operations. The DVB demux and DVR device are enough
            // to be opened in read only mode.
            return ParcelFileDescriptor.open(new File(deviceFileName),
                    TvInputManager.DVB_DEVICE_FRONTEND == device
                            ? ParcelFileDescriptor.MODE_READ_WRITE
                            : ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e) {
            return null;
        }
    } finally {
        Binder.restoreCallingIdentity(identity);
    }
}
 
Example 16
Source File: FileProvider.java    From letv with Apache License 2.0 4 votes vote down vote up
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    return ParcelFileDescriptor.open(this.mStrategy.getFileForUri(uri), modeToMode(mode));
}
 
Example 17
Source File: Request.java    From HypFacebook with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * 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
 * @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);
}
 
Example 18
Source File: FileProvider.java    From YalpStore with GNU General Public License v2.0 3 votes vote down vote up
/**
 * By default, FileProvider automatically returns the
 * {@link ParcelFileDescriptor} for a file associated with a <code>content://</code>
 * {@link Uri}. To get the {@link ParcelFileDescriptor}, call
 * {@link android.content.ContentResolver#openFileDescriptor(Uri, String)
 * ContentResolver.openFileDescriptor}.
 *
 * To override this method, you must provide your own subclass of FileProvider.
 *
 * @param uri A content URI associated with a file, as returned by
 * {@link #getUriForFile(Context, String, File) getUriForFile()}.
 * @param mode Access mode for the file. May be "r" for read-only access, "rw" for read and
 * write access, or "rwt" for read and write access that truncates any existing file.
 * @return A new {@link ParcelFileDescriptor} with which you can access the file.
 */
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    // ContentProvider has already checked granted permissions
    final File file = mStrategy.getFileForUri(uri);
    final int fileMode = modeToMode(mode);
    return ParcelFileDescriptor.open(file, fileMode);
}
 
Example 19
Source File: FileProvider.java    From guideshow with MIT License 3 votes vote down vote up
/**
 * By default, FileProvider automatically returns the
 * {@link ParcelFileDescriptor} for a file associated with a <code>content://</code>
 * {@link Uri}. To get the {@link ParcelFileDescriptor}, call
 * {@link android.content.ContentResolver#openFileDescriptor(Uri, String)
 * ContentResolver.openFileDescriptor}.
 *
 * To override this method, you must provide your own subclass of FileProvider.
 *
 * @param uri A content URI associated with a file, as returned by
 * {@link #getUriForFile(Context, String, File) getUriForFile()}.
 * @param mode Access mode for the file. May be "r" for read-only access, "rw" for read and
 * write access, or "rwt" for read and write access that truncates any existing file.
 * @return A new {@link ParcelFileDescriptor} with which you can access the file.
 */
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    // ContentProvider has already checked granted permissions
    final File file = mStrategy.getFileForUri(uri);
    final int fileMode = modeToMode(mode);
    return ParcelFileDescriptor.open(file, fileMode);
}
 
Example 20
Source File: Request.java    From aws-mobile-self-paced-labs-samples with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 * @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);
}