use of android.os.ParcelFileDescriptor in project GalleryFinal by pengjianbo.
the class CropUtil method getFromMediaUriPfd.
@Nullable
private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) {
if (uri == null)
return null;
FileInputStream input = null;
FileOutputStream output = null;
try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd.getFileDescriptor();
input = new FileInputStream(fd);
String tempFilename = getTempFilename(context);
output = new FileOutputStream(tempFilename);
int read;
byte[] bytes = new byte[4096];
while ((read = input.read(bytes)) != -1) {
output.write(bytes, 0, read);
}
return new File(tempFilename);
} catch (IOException ignored) {
// Nothing we can do
} finally {
closeSilently(input);
closeSilently(output);
}
return null;
}
use of android.os.ParcelFileDescriptor in project chromeview by pwnall.
the class ChildProcessConnection method doConnectionSetup.
/**
* Called when the connection parameters have been set, and a connection has been established
* (as signaled by onServiceConnected), or if the connection failed (mService will be false).
*/
private void doConnectionSetup() {
TraceEvent.begin();
assert mServiceConnectComplete && mConnectionParams != null;
// Capture the callback before it is potentially nulled in unbind().
Runnable onConnectionCallback = mConnectionParams.mOnConnectionCallback;
if (onConnectionCallback == null) {
unbind();
} else if (mService != null) {
Bundle bundle = new Bundle();
bundle.putStringArray(EXTRA_COMMAND_LINE, mConnectionParams.mCommandLine);
FileDescriptorInfo[] fileInfos = mConnectionParams.mFilesToBeMapped;
ParcelFileDescriptor[] parcelFiles = new ParcelFileDescriptor[fileInfos.length];
for (int i = 0; i < fileInfos.length; i++) {
if (fileInfos[i].mFd == -1) {
// If someone provided an invalid FD, they are doing something wrong.
Log.e(TAG, "Invalid FD (id=" + fileInfos[i].mId + ") for process connection, " + "aborting connection.");
return;
}
String idName = EXTRA_FILES_PREFIX + i + EXTRA_FILES_ID_SUFFIX;
String fdName = EXTRA_FILES_PREFIX + i + EXTRA_FILES_FD_SUFFIX;
if (fileInfos[i].mAutoClose) {
// Adopt the FD, it will be closed when we close the ParcelFileDescriptor.
parcelFiles[i] = ParcelFileDescriptor.adoptFd(fileInfos[i].mFd);
} else {
try {
parcelFiles[i] = ParcelFileDescriptor.fromFd(fileInfos[i].mFd);
} catch (IOException e) {
Log.e(TAG, "Invalid FD provided for process connection, aborting connection.", e);
return;
}
}
bundle.putParcelable(fdName, parcelFiles[i]);
bundle.putInt(idName, fileInfos[i].mId);
}
// Add the CPU properties now.
bundle.putInt(EXTRA_CPU_COUNT, CpuFeatures.getCount());
bundle.putLong(EXTRA_CPU_FEATURES, CpuFeatures.getMask());
try {
mPID = mService.setupConnection(bundle, mConnectionParams.mCallback);
} catch (android.os.RemoteException re) {
Log.e(TAG, "Failed to setup connection.", re);
}
// We proactivley close the FDs rather than wait for GC & finalizer.
try {
for (ParcelFileDescriptor parcelFile : parcelFiles) {
if (parcelFile != null)
parcelFile.close();
}
} catch (IOException ioe) {
Log.w(TAG, "Failed to close FD.", ioe);
}
}
mConnectionParams = null;
if (onConnectionCallback != null) {
onConnectionCallback.run();
}
TraceEvent.end();
}
use of android.os.ParcelFileDescriptor in project robolectric by robolectric.
the class ShadowParcelFileDescriptor method open.
@Implementation
public static ParcelFileDescriptor open(File file, int mode) throws FileNotFoundException {
ParcelFileDescriptor pfd;
try {
Constructor<ParcelFileDescriptor> constructor = ParcelFileDescriptor.class.getDeclaredConstructor(FileDescriptor.class);
pfd = constructor.newInstance(new FileDescriptor());
} catch (Exception e) {
throw new RuntimeException(e);
}
Shadows.shadowOf(pfd).file = new RandomAccessFile(file, mode == ParcelFileDescriptor.MODE_READ_ONLY ? "r" : "rw");
return pfd;
}
use of android.os.ParcelFileDescriptor in project robolectric by robolectric.
the class ShadowParcelFileDescriptorTest method testStatSize_writtenFile.
@Test
public void testStatSize_writtenFile() throws Exception {
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
assertThat(pfd).isNotNull();
assertThat(pfd.getFileDescriptor().valid()).isTrue();
FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor());
os.write(5);
// One byte.
assertThat(pfd.getStatSize()).isEqualTo(1);
os.close();
}
use of android.os.ParcelFileDescriptor in project robolectric by robolectric.
the class ShadowParcelFileDescriptorTest method testOpens_canWriteWritableFile.
@Test
public void testOpens_canWriteWritableFile() throws Exception {
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
assertThat(pfd).isNotNull();
assertThat(pfd.getFileDescriptor().valid()).isTrue();
FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor());
os.write(5);
os.close();
}
Aggregations