Example usage for android.os ParcelFileDescriptor close

List of usage examples for android.os ParcelFileDescriptor close

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Close the ParcelFileDescriptor.

Usage

From source file:Main.java

public static void closeSilently(ParcelFileDescriptor c) {
    if (c == null)
        return;/*from  ww w  .ja  v  a 2 s.  co m*/
    try {
        c.close();
    } catch (Throwable t) {
        // do nothing
    }
}

From source file:Main.java

public static void closeSilently(ParcelFileDescriptor fd) {
    try {/*from  ww w  . j ava2  s  . c o  m*/
        if (fd != null)
            fd.close();
    } catch (Throwable t) {
        Log.w(TAG, "fail to close", t);
    }
}

From source file:Main.java

public static void closeSilently(final ParcelFileDescriptor c) {
    if (c == null)
        return;/*from   w w w.j a v a  2 s .  co  m*/
    try {
        c.close();
    } catch (final Throwable t) {
    }
}

From source file:Main.java

public static void closeSilently(ParcelFileDescriptor c) {
    if (c == null)
        return;/*from  w  w  w . ja va 2 s  .  co m*/
    try {
        c.close();
    } catch (Throwable t) {
        Log.w(TAG, "fail to close", t);
    }
}

From source file:Main.java

public static long getFileSize(Context context, Uri uri) {
    try {/*from w w w.  jav  a2s  . co m*/
        ParcelFileDescriptor fd = context.getContentResolver().openFileDescriptor(uri, "r");
        long size = fd.getStatSize();
        fd.close();
        return size;
    } catch (Throwable exception) {
        return 0;
    }
}

From source file:Main.java

public static void closeQuietly(ParcelFileDescriptor p) {
    if (p != null) {
        try {/* www. j  av a2s.  c o m*/
            p.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static boolean isUriValid(Uri uri, ContentResolver resolver) {
    if (uri == null)
        return false;

    try {/*from  ww  w.  j  av a2s. c  o m*/
        ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
        if (pfd == null) {
            Log.e(TAG, "Fail to open URI. URI=" + uri);
            return false;
        }
        pfd.close();
    } catch (IOException ex) {
        return false;
    }
    return true;
}

From source file:Main.java

public static Bitmap getBitmapFromUri(Uri uri, Context context) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {/*from   ww  w. java  2s .  c  om*/

        parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
    } catch (Exception e) {
        Log.e(TAG, "Failed to load image.", e);
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Error closing ParcelFile Descriptor");
        }
    }
}

From source file:com.github.michalbednarski.intentslab.Utils.java

@TargetApi(13) // Function handles all supported api levels
public static InputStream dumpSystemService(Context context, String serviceName, final String[] arguments)
        throws Exception {
    // Check if we have permission to invoke dump from our process
    final boolean canDumpLocally = context.getPackageManager().checkPermission(android.Manifest.permission.DUMP,
            context.getPackageName()) == PackageManager.PERMISSION_GRANTED;

    // On versions without createPipe() just execute dumpsys
    if (android.os.Build.VERSION.SDK_INT < 9) {
        if (!canDumpLocally) {
            throw new Exception("Dumping is not supported on this system version");
        }//from ww w  . j av  a  2 s  . c o  m
        String[] progArray = new String[arguments != null ? 2 + arguments.length : 2];
        progArray[0] = "dumpsys";
        progArray[1] = serviceName;
        if (arguments != null) {
            System.arraycopy(arguments, 0, progArray, 2, arguments.length);
        }
        return Runtime.getRuntime().exec(progArray).getInputStream();
    }

    // Get service
    final Class<?> serviceManager = Class.forName("android.os.ServiceManager");
    final IBinder service = (IBinder) serviceManager.getMethod("getService", String.class).invoke(null,
            serviceName);

    // Check permissions and get remote interface if needed
    IRemoteInterface remoteInterface = null;
    if (!canDumpLocally) {
        remoteInterface = RunAsManager.getRemoteInterfaceForSystemDebuggingCommands();
        if (remoteInterface == null) {
            throw new SecurityException("Process has no permission to dump services");
        }
    }

    // Create pipe, write(pipe[0]) -> read(pipe[1])
    final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    final ParcelFileDescriptor readablePipe = pipe[0];
    final ParcelFileDescriptor writablePipe = pipe[1];

    try {
        // Execute dump
        if (canDumpLocally) {
            if (android.os.Build.VERSION.SDK_INT >= 13) {
                service.dumpAsync(writablePipe.getFileDescriptor(), arguments);
                writablePipe.close();
            } else {
                (new Thread() {
                    @Override
                    public void run() {
                        try {
                            service.dump(writablePipe.getFileDescriptor(), arguments);
                            writablePipe.close();
                        } catch (Exception e) {
                            // TODO: can we handle this?
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } else {
            remoteInterface.dumpServiceAsync(service, writablePipe, arguments);
            writablePipe.close();
        }
        // If anything went wrong, close pipe and rethrow
    } catch (Throwable e) {
        readablePipe.close();
        writablePipe.close();
        throwUnchecked(e);
        throw new Error(); // Unreachable
    }

    // Return stream that will ensure closing fd
    return new FileInputStream(readablePipe.getFileDescriptor()) {
        @Override
        public void close() throws IOException {
            super.close();
            readablePipe.close();
        }
    };
}

From source file:com.maskyn.fileeditorpro.task.SaveFileTask.java

private void writeUri(Uri uri, String newContent, String encoding) throws IOException {
    ParcelFileDescriptor pfd = activity.getContentResolver().openFileDescriptor(uri, "w");
    FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
    fileOutputStream.write(newContent.getBytes(Charset.forName(encoding)));
    fileOutputStream.close();//from ww  w .  j av  a  2s.com
    pfd.close();
}