List of usage examples for android.content ContentResolver openInputStream
public final @Nullable InputStream openInputStream(@NonNull Uri uri) throws FileNotFoundException
From source file:Main.java
/** * Gets binary from uri.//from w w w. j a va 2 s . co m * * @param context context * @param uri uri * @return byte[] or null on error */ public static byte[] getContentData(final Context context, final String uri) { if (uri == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = null; byte[] buf = new byte[BUF_SIZE]; int len; try { ContentResolver r = context.getContentResolver(); in = r.openInputStream(Uri.parse(uri)); while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } return out.toByteArray(); } catch (IOException e) { return null; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static InputStream createWallpaperInputStream(Context context, String preferenceValue) throws FileNotFoundException { File file = new File(preferenceValue); if (file.exists()) { return new FileInputStream(file); }//from w w w . jav a 2 s .co m Uri uri = Uri.parse(preferenceValue); ContentResolver contentResolver = context.getContentResolver(); try { return contentResolver.openInputStream(uri); } catch (SecurityException e) { Log.i("ThemingUtils", "unable to open background image", e); FileNotFoundException fileNotFoundException = new FileNotFoundException(e.getMessage()); fileNotFoundException.initCause(e); throw fileNotFoundException; } }
From source file:Main.java
/** * Convert an image to a byte array//from w ww .ja va 2s . co m * @param uri * @param context * @return */ public static byte[] convertImageToByte(Uri uri, Context context) { byte[] data = null; try { ContentResolver cr = context.getContentResolver(); InputStream inputStream = cr.openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); data = baos.toByteArray(); if (inputStream != null) { try { inputStream.close(); } catch (Exception e2) { } } if (baos != null) { try { baos.close(); } catch (Exception e2) { } } } catch (FileNotFoundException e) { e.printStackTrace(); } return data; }
From source file:Main.java
public static Drawable changeLocalAvatar(Context context, Uri uri) { ContentResolver cr = context.getContentResolver(); Bitmap bitmap = null;//from w ww . j ava 2 s. c om try { bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); } return new BitmapDrawable(bitmap); }
From source file:Main.java
public static Bitmap decodeUriAsBitmap(Context context, Uri uri, BitmapFactory.Options options) { Bitmap result = null;//from www. ja v a 2 s .c o m if (uri != null) { ContentResolver cr = context.getContentResolver(); InputStream inputStream = null; try { inputStream = cr.openInputStream(uri); result = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return result; }
From source file:Main.java
public static void decodeSize(ContentResolver contentResolver, Uri uri, Point out) throws IOException { Options opts = new Options(); opts.inJustDecodeBounds = true;/*w w w . j a va 2 s. c om*/ BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, opts); out.set(opts.outWidth, opts.outHeight); }
From source file:Main.java
public static InputStream getISFromURI(Context context, Uri contentURI) { ContentResolver res = context.getContentResolver(); Uri uri = Uri.parse(contentURI.toString()); InputStream is = null;/*from w w w. j a v a2s. c o m*/ try { is = res.openInputStream(uri); } catch (FileNotFoundException e) { jlog(e); } return is; }
From source file:Main.java
/** * Read bytes.//from w ww. j a v a2s . c om * * @param uri the uri * @param resolver the resolver * @return the byte[] * @throws java.io.IOException Signals that an I/O exception has occurred. */ private static byte[] readBytes(Uri uri, ContentResolver resolver) throws IOException { // this dynamically extends to take the bytes you read InputStream inputStream = resolver.openInputStream(uri); ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); // this is storage overwritten on each iteration with bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; // we need to know how may bytes were read to write them to the // byteBuffer int len = 0; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } // and then we can return your byte array. return byteBuffer.toByteArray(); }
From source file:Main.java
/** * Decode downloaded big picture.// w ww . ja v a 2 s. c om * @param context any application context. * @param downloadId downloaded picture identifier. * @return decoded bitmap if successful, null on error. */ public static Bitmap getBigPicture(Context context, long downloadId) { /* Decode bitmap */ InputStream stream = null; try { /* * Query download manager to get file. FIXME For an unknown reason, using the file descriptor * fails after APK build with ProGuard, we use stream instead. */ DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = downloadManager.getUriForDownloadedFile(downloadId); ContentResolver contentResolver = context.getContentResolver(); stream = contentResolver.openInputStream(uri); /* * Bitmaps larger than 2048 in any dimension are likely to cause OutOfMemoryError in the * NotificationManager or even here. Plus some devices simply don't accept such images: the * notification manager can drop the notification without any way to check that via API. To * avoid the problem, sub sample the image in an efficient way (not using Bitmap matrix * scaling after decoding the bitmap with original size: it could run out of memory when * decoding the full image). FIXME There is center cropping applied by the NotificationManager * on the bitmap we provide, we can't avoid it, see * https://code.google.com/p/android/issues/detail?id=58318. */ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; options.inSampleSize = 1; options.inPreferQualityOverSpeed = true; /* Decode dimensions */ BitmapFactory.decodeStream(stream, null, options); int maxDim = Math.max(options.outWidth, options.outHeight); /* Compute sub sample size (it must be a power of 2) */ while (maxDim > 2048) { options.inSampleSize <<= 1; maxDim >>= 1; } /* Decode actual bitmap */ options.inJustDecodeBounds = false; stream.close(); stream = contentResolver.openInputStream(uri); return BitmapFactory.decodeStream(stream, null, options); } catch (Throwable t) { /* Abort, causes are likely FileNotFoundException or OutOfMemoryError */ return null; } finally { /* Silently close stream */ if (stream != null) try { stream.close(); } catch (IOException e) { } } }
From source file:com.richtodd.android.repository.JSONUtility.java
public static JSONObject loadJSONObject(ContentResolver contentResolver, Uri uri) throws RepositoryException { try {/*from w w w . jav a2s . c o m*/ InputStream in = contentResolver.openInputStream(uri); try { return loadJSONObject(in); } finally { in.close(); } } catch (FileNotFoundException ex) { throw new RepositoryException(ex); } catch (IOException ex) { throw new RepositoryException(ex); } }