Search in sources :

Example 26 with ParcelFileDescriptor

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) Nullable(android.support.annotation.Nullable)

Example 27 with ParcelFileDescriptor

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();
}
Also used : Bundle(android.os.Bundle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

Example 28 with ParcelFileDescriptor

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;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Implementation(org.robolectric.annotation.Implementation)

Example 29 with ParcelFileDescriptor

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Test(org.junit.Test)

Example 30 with ParcelFileDescriptor

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Test(org.junit.Test)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17