Example usage for android.content ContentResolver openFileDescriptor

List of usage examples for android.content ContentResolver openFileDescriptor

Introduction

In this page you can find the example usage for android.content ContentResolver openFileDescriptor.

Prototype

public final @Nullable ParcelFileDescriptor openFileDescriptor(@NonNull Uri uri, @NonNull String mode)
        throws FileNotFoundException 

Source Link

Document

Open a raw file descriptor to access data under a URI.

Usage

From source file:Main.java

private static ParcelFileDescriptor makeInputStream(Uri uri, ContentResolver cr) {
    try {//from w w w  . j a v  a2s .co m
        return cr.openFileDescriptor(uri, "r");
    } catch (IOException ex) {
        return null;
    }
}

From source file:Main.java

/**
 * Make a bitmap from a given Uri./*from   w  ww .j  a v a 2  s . c  o  m*/
 * 
 * @param uri
 */

@SuppressWarnings("unused")
private static ParcelFileDescriptor makeInputStream(Uri uri, ContentResolver cr) {
    try {
        return cr.openFileDescriptor(uri, "r");
    } catch (IOException ex) {
        return null;
    }
}

From source file:Main.java

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

    try {//  w ww .  ja  va  2 s .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

/**
 * Helper method to open a content URI and returns the ParcelFileDescriptor.
 *
 * @param context {@link Context} in interest.
 * @param uriString the content URI to open.
 * @return ParcelFileDescriptor of the content URI, or NULL if the file does not exist.
 *//*w w w.j  a  v  a 2 s . c o m*/
private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = Uri.parse(uriString);

    ParcelFileDescriptor pfd = null;
    try {
        pfd = resolver.openFileDescriptor(uri, "r");
    } catch (FileNotFoundException e) {
        Log.w(TAG, "Cannot find content uri: " + uriString, e);
    } catch (SecurityException e) {
        Log.w(TAG, "Cannot open content uri: " + uriString, e);
    } catch (IllegalArgumentException e) {
        Log.w(TAG, "Unknown content uri: " + uriString, e);
    } catch (IllegalStateException e) {
        Log.w(TAG, "Unknown content uri: " + uriString, e);
    }
    return pfd;
}

From source file:Main.java

public static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the
    // ImageView//from ww  w .  jav  a2 s. co m
    // used to display this drawable. Take it into account now, so we don't
    // have to
    // scale later.
    w -= 1;
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            int sampleSize = 1;

            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }

            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // Bitmap.createScaledBitmap() can return the same
                    // bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }

            return b;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:Main.java

@Nullable
private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    FileInputStream input = null;
    FileOutputStream output = null;
    try {/*from  ww w .  ja  va  2 s .  c o  m*/
        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;
}

From source file:Main.java

private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    FileInputStream input = null;
    FileOutputStream output = null;
    try {//from   ww  w  .  j  ava 2  s  .  c o  m
        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;
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Load a Bitmap from an image file.//from   w w w  .  ja  v  a 2s.co m
 *
 * @param context   The Context
 * @param uri       The Uri to the image file
 * @param reqWidth  The requested width of the decoded Bitmap
 * @param reqHeight The requested height of the decoded Bitmap
 * @return A Bitmap
 */
@Nullable
public static Bitmap loadBitmap(@NonNull Context context, @NonNull Uri uri, int reqWidth, int reqHeight) {
    final ContentResolver cr = context.getContentResolver();
    try {
        final ParcelFileDescriptor parcelFileDescriptor = cr.openFileDescriptor(uri, "r");
        if (parcelFileDescriptor == null) {
            Log.w(TAG, "Unable to open " + uri.toString());
            return null;
        }
        try {
            final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            final Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);

            opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);
            opts.inJustDecodeBounds = false;

            final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
            if ("image/jpeg".equals(opts.outMimeType)) {
                return rotatePhoto(context, uri, bitmap);
            }
            return bitmap;
        } catch (OutOfMemoryError e) {
            Log.e(TAG, "Out of memory", e);
        } finally {
            parcelFileDescriptor.close();
        }
    } catch (FileNotFoundException e) {
        Log.w(TAG, "File not found: " + uri.toString());
    } catch (SecurityException e) {
        Log.w(TAG, "Permission denied for Uri: " + uri.toString());
    } catch (IOException e) {
        Log.e(TAG, "Failed to load bitmap", e);
    }
    return null;
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static OutputStream openOutputStream(Context context, DocumentFile f) throws IOException {
    ContentResolver cr = context.getContentResolver();
    ParcelFileDescriptor pfd = cr.openFileDescriptor(f.getUri(), "rw");

    int fd = pfd.detachFd(); // this trick the internal system to trigger the media scanner on nothing
    pfd = ParcelFileDescriptor.adoptFd(fd);

    return new AutoSyncOutputStream(pfd);
}

From source file:com.serenegiant.testmediastore.MediaStorePhotoAdapter.java

private static final Bitmap getImage(ContentResolver cr, long id, int requestWidth, int requestHeight)
        throws FileNotFoundException, IOException {

    Bitmap result = null;/*  w  w w .  j a  va2s  .c  o  m*/
    final ParcelFileDescriptor pfd = cr.openFileDescriptor(
            ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id), "r");
    if (pfd != null) {
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            // just decorde to get image size
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, options);
            // calculate sub-sampling
            options.inSampleSize = calcSampleSize(options, requestWidth, requestHeight);
            options.inJustDecodeBounds = false;
            result = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, options);
        } finally {
            pfd.close();
        }
    }
    return result;
}