Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); ContentResolver res = context.getContentResolver(); InputStream in = res.openInputStream(uri); Bitmap artwork = BitmapFactory.decodeStream(in);
@Override public InputStream open() throws IOException { return context.getContentResolver().openInputStream(uri); }
Source getSource(Uri uri) throws FileNotFoundException { ContentResolver contentResolver = context.getContentResolver(); InputStream inputStream = contentResolver.openInputStream(uri); if (inputStream == null) { throw new FileNotFoundException("can't open input stream, uri: " + uri); } return Okio.source(inputStream); }
public static void copyFromUri(@NonNull Context context, @NonNull OutputStream outputStream, @NonNull Uri uri) throws IOException { try (final InputStream inputStream = context.getContentResolver().openInputStream(uri)) { if (inputStream == null) { throw new FileNotFoundException("Could not open " + uri.toString()); } final byte[] buffer = new byte[ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } } }
int getOrientation(Uri uri) { InputStream is = null; try { is = contentResolver.openInputStream(uri); return ImageHeaderParserUtils.getOrientation(parsers, is, byteArrayPool); // PMD.AvoidCatchingNPE framework method openInputStream can throw NPEs. } catch (@SuppressWarnings("PMD.AvoidCatchingNPE") IOException | NullPointerException e) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Failed to open uri: " + uri, e); } } finally { if (is != null) { try { is.close(); } catch (IOException e) { // Ignored. } } } return ImageHeaderParser.UNKNOWN_ORIENTATION; }
private static BitmapFactory.Options getOptimalSizeOptions( Context context, Uri bitmapUri, int reqWidth, int reqHeight) throws FileNotFoundException { InputStream is = context.getContentResolver().openInputStream(bitmapUri); BitmapFactory.Options result = new BitmapFactory.Options(); result.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, result); result.inJustDecodeBounds = false; result.inSampleSize = calculateInSampleSize(result, reqWidth, reqHeight); return result; }
protected int getExifOrientation(Uri uri) throws IOException { ContentResolver contentResolver = context.getContentResolver(); InputStream inputStream = null; try { inputStream = contentResolver.openInputStream(uri); if (inputStream == null) { throw new FileNotFoundException("can't open input stream, uri: " + uri); } ExifInterface exifInterface = new ExifInterface(inputStream); return exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ignored) { } } } }
public static int getExifOrientation(@NonNull Context context, @NonNull Uri imageUri) { int orientation = ExifInterface.ORIENTATION_UNDEFINED; try { InputStream stream = context.getContentResolver().openInputStream(imageUri); if (stream == null) { return orientation; } orientation = new ImageHeaderParser(stream).getOrientation(); close(stream); } catch (IOException e) { Log.e(TAG, "getExifOrientation: " + imageUri.toString(), e); } return orientation; }
/** * Retrieves {@link InputStream} of image by URI (image is accessed using {@link ContentResolver}). * * @param imageUri Image URI * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object) * DisplayImageOptions.extraForDownloader(Object)}; can be null * @return {@link InputStream} of image * @throws FileNotFoundException if the provided URI could not be opened */ protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException { ContentResolver res = context.getContentResolver(); Uri uri = Uri.parse(imageUri); if (isVideoContentUri(uri)) { // video thumbnail Long origId = Long.valueOf(uri.getLastPathSegment()); Bitmap bitmap = MediaStore.Video.Thumbnails .getThumbnail(res, origId, MediaStore.Images.Thumbnails.MINI_KIND, null); if (bitmap != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, bos); return new ByteArrayInputStream(bos.toByteArray()); } } else if (imageUri.startsWith(CONTENT_CONTACTS_URI_PREFIX)) { // contacts photo return getContactPhotoStream(uri); } return res.openInputStream(uri); }
private InputStream loadResourceFromUri(Uri uri, ContentResolver contentResolver) throws FileNotFoundException { switch (URI_MATCHER.match(uri)) { case ID_CONTACTS_CONTACT: return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_LOOKUP: case ID_LOOKUP_BY_PHONE: // If it was a Lookup uri then resolve it first, then continue loading the contact uri. uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); if (uri == null) { throw new FileNotFoundException("Contact cannot be found"); } return openContactPhotoInputStream(contentResolver, uri); case ID_CONTACTS_THUMBNAIL: case ID_CONTACTS_PHOTO: case UriMatcher.NO_MATCH: default: return contentResolver.openInputStream(uri); } }
private Source getSource(Uri uri) throws IOException { ContentResolver contentResolver = context.getContentResolver(); InputStream is; switch (matcher.match(uri)) { case ID_LOOKUP: uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); if (uri == null) { throw new IOException("no contact found"); } // Resolved the uri to a contact uri, intentionally fall through to process the resolved uri case ID_CONTACT: is = openContactPhotoInputStream(contentResolver, uri, true); break; case ID_THUMBNAIL: case ID_DISPLAY_PHOTO: is = contentResolver.openInputStream(uri); break; default: throw new IllegalStateException("Invalid uri: " + uri); } if (is == null) { throw new FileNotFoundException("can't open input stream, uri: " + uri); } return Okio.source(is); } }
public InputStream open(Uri uri) throws FileNotFoundException { String path = getPath(uri); if (TextUtils.isEmpty(path)) { return null; } File file = service.get(path); if (!isValid(file)) { return null; } Uri thumbnailUri = Uri.fromFile(file); try { return contentResolver.openInputStream(thumbnailUri); // PMD.AvoidCatchingNPE framework method openInputStream can throw NPEs. } catch (@SuppressWarnings("PMD.AvoidCatchingNPE") NullPointerException e) { throw (FileNotFoundException) new FileNotFoundException("NPE opening uri: " + uri + " -> " + thumbnailUri).initCause(e); } }
@Test(expected = UnsupportedOperationException.class) public void openInputStream_shouldReturnAnInputStreamThatExceptionsOnRead() throws Exception { InputStream inputStream = contentResolver.openInputStream(uri21); inputStream.read(); }
private void setAlbumart() { albumTitle.setText(playlistname); albumArtSmall.setImageURI(Uri.parse(albumPath)); final Drawable drawable; try { drawable = Drawable.createFromStream(getContext().getContentResolver().openInputStream(Uri.parse(albumPath)), null); new setBlurredAlbumArt().execute(ImageUtils.getBitmapFromDrawable(drawable)); } catch (Exception e) { } }
private void setAlbumart() { albumTitle.setText(playlistname); albumArtSmall.setImageURI(Uri.parse(albumPath)); final Drawable drawable; try { drawable = Drawable.createFromStream(PlaylistDetailActivity.this.getContentResolver().openInputStream(Uri.parse(albumPath)), null); new setBlurredAlbumArt().execute(ImageUtils.getBitmapFromDrawable(drawable)); } catch (Exception e) { } }
@Test public void openInputStream_returnsPreRegisteredStream() throws Exception { shadowContentResolver.registerInputStream(uri21, new ByteArrayInputStream("ourStream".getBytes(UTF_8))); InputStream inputStream = contentResolver.openInputStream(uri21); byte[] data = new byte[9]; inputStream.read(data); assertThat(new String(data, UTF_8)).isEqualTo("ourStream"); }
@Test public void decodeStream_shouldSetDescriptionAndCreatedFrom() throws Exception { InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); ShadowBitmap shadowBitmap = shadowOf(bitmap); assertEquals("Bitmap for content:/path", shadowBitmap.getDescription()); assertEquals(inputStream, shadowBitmap.getCreatedFromStream()); assertEquals(100, bitmap.getWidth()); assertEquals(100, bitmap.getHeight()); bitmap.getPixels(new int[bitmap.getHeight() * bitmap.getWidth()], 0, 0, 0, 0, bitmap.getWidth(), bitmap.getHeight()); }
@Test public void decodeStream_shouldSetDescriptionWithNullOptions() throws Exception { InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, null); assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); assertEquals(100, bitmap.getWidth()); assertEquals(100, bitmap.getHeight()); }
@Test public void decodeStream_shouldGetWidthAndHeightFromHints() throws Exception { ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse("content:/path"), 123, 456); InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); assertEquals(123, bitmap.getWidth()); assertEquals(456, bitmap.getHeight()); }
@Test public void decodeResourceStream_canTakeOptions() throws Exception { BitmapFactory.Options options = new BitmapFactory.Options(); InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); options.inSampleSize = 100; Bitmap bitmap = BitmapFactory.decodeResourceStream( context.getResources(), null, inputStream, null, options); assertEquals(true, shadowOf(bitmap).getDescription().contains("inSampleSize=100")); }