下面列出了android.os.ParcelFileDescriptor#open ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
}
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;
}
@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);
}
}
@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;
}
}
@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);
}
}
@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);
}
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;
}
}
@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);
}
@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();
}
@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);
}
@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);
}
@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);
}
}
@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);
}
}
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;
}
}
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
return ParcelFileDescriptor.open(this.mStrategy.getFileForUri(uri), modeToMode(mode));
}
@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);
}
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}