本文整理汇总了Java中android.os.ParcelFileDescriptor.AutoCloseInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ParcelFileDescriptor.AutoCloseInputStream方法的具体用法?Java ParcelFileDescriptor.AutoCloseInputStream怎么用?Java ParcelFileDescriptor.AutoCloseInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.os.ParcelFileDescriptor
的用法示例。
在下文中一共展示了ParcelFileDescriptor.AutoCloseInputStream方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initialize
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
private void initialize()
throws FileNotFoundException {
ParcelFileDescriptor fileDescriptor = null;
try {
if (Utility.isFileUri(videoUri)) {
fileDescriptor = ParcelFileDescriptor.open(
new File(videoUri.getPath()),
ParcelFileDescriptor.MODE_READ_ONLY);
videoSize = fileDescriptor.getStatSize();
videoStream = new ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor);
} else if (Utility.isContentUri(videoUri)) {
videoSize = Utility.getContentSize(videoUri);
videoStream = FacebookSdk
.getApplicationContext()
.getContentResolver()
.openInputStream(videoUri);
} else {
throw new FacebookException("Uri must be a content:// or file:// uri");
}
} catch (FileNotFoundException e) {
Utility.closeQuietly(videoStream);
throw e;
}
}
示例2: fileMd5Hash
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
@Rpc(description = "Compute MD5 hash on a content URI. Return the MD5 has has a hex string.")
public String fileMd5Hash(String uri) throws IOException, NoSuchAlgorithmException {
Uri uri_ = Uri.parse(uri);
ParcelFileDescriptor pfd = mContext.getContentResolver().openFileDescriptor(uri_, "r");
MessageDigest md = MessageDigest.getInstance("MD5");
int length = (int) pfd.getStatSize();
byte[] buf = new byte[length];
ParcelFileDescriptor.AutoCloseInputStream stream =
new ParcelFileDescriptor.AutoCloseInputStream(pfd);
DigestInputStream dis = new DigestInputStream(stream, md);
try {
dis.read(buf, 0, length);
return Utils.bytesToHexString(md.digest());
} finally {
dis.close();
stream.close();
}
}
示例3: createForParcelFileDescriptor
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
/**
* Creates a DocumentsArchive instance for opening, browsing and accessing
* documents within the archive passed as a file descriptor.
*
* <p>Note, that this method should be used only if the document does not exist
* on the local storage. A snapshot file will be created, which may be slower
* and consume significant resources, in contrast to using
* {@see createForLocalFile(Context, File, String, char, Uri}.
*
* @param context Context of the provider.
* @param descriptor File descriptor for the archive's contents.
* @param documentId ID of the archive document.
* @param idDelimiter Delimiter for constructing IDs of documents within the archive.
* The delimiter must never be used for IDs of other documents.
* @param Uri notificationUri Uri for notifying that the archive file has changed.
* @see createForLocalFile(Context, File, String, char, Uri)
*/
public static DocumentArchive createForParcelFileDescriptor(
Context context, ParcelFileDescriptor descriptor, String documentId,
char idDelimiter, @Nullable Uri notificationUri)
throws IOException {
File snapshotFile = null;
try {
// Create a copy of the archive, as ZipFile doesn't operate on streams.
// Moreover, ZipInputStream would be inefficient for large files on
// pipes.
snapshotFile = File.createTempFile("android.support.provider.snapshot{",
"}.zip", context.getCacheDir());
try {
final FileOutputStream outputStream =
new ParcelFileDescriptor.AutoCloseOutputStream(
ParcelFileDescriptor.open(
snapshotFile, ParcelFileDescriptor.MODE_WRITE_ONLY));
final ParcelFileDescriptor.AutoCloseInputStream inputStream =
new ParcelFileDescriptor.AutoCloseInputStream(descriptor);
final byte[] buffer = new byte[32 * 1024];
int bytes;
while ((bytes = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytes);
}
outputStream.flush();
return new DocumentArchive(context, snapshotFile, documentId, idDelimiter,
notificationUri);
} catch (Exception e){
CrashReportingManager.logException(e);
return null;
}
} finally {
// On UNIX the file will be still available for processes which opened it, even
// after deleting it. Remove it ASAP, as it won't be used by anyone else.
if (snapshotFile != null) {
snapshotFile.delete();
}
}
}
示例4: pipeTo
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
@SuppressWarnings("unused")
public static ParcelFileDescriptor pipeTo(OutputStream outputStream)
throws IOException {
final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
final InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
new TransferThread(input, outputStream).start();
return pipe[1];
}
示例5: NativePipe
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
public NativePipe() {
try {
ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]) {
private boolean closed = false;
@Override
public void close() throws IOException {
if (closed) return;
closed = true;
super.close();
outputStream.close();
}
};
outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]) {
private boolean closed = false;
@Override
public void close() throws IOException {
if (closed) return;
closed = true;
super.close();
inputStream.close();
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例6: grantWidgetPermission
import android.os.ParcelFileDescriptor; //导入方法依赖的package包/类
/**
* Grants the launcher permission to bind widgets.
*/
protected void grantWidgetPermission() throws IOException {
// Check bind widget permission
if (mTargetContext.getPackageManager().checkPermission(
mTargetPackage, android.Manifest.permission.BIND_APPWIDGET)
!= PackageManager.PERMISSION_GRANTED) {
ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation().executeShellCommand(
"appwidget grantbind --package " + mTargetPackage);
// Read the input stream fully.
FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
while (fis.read() != -1);
fis.close();
}
}