Example usage for android.content Context getContentResolver

List of usage examples for android.content Context getContentResolver

Introduction

In this page you can find the example usage for android.content Context getContentResolver.

Prototype

public abstract ContentResolver getContentResolver();

Source Link

Document

Return a ContentResolver instance for your application's package.

Usage

From source file:com.kku.apps.pricesearch.util.Utils.java

public static Cursor readFavorites(Context context, ListItem item) {

    final ContentResolver cr = context.getContentResolver();
    final Cursor c = cr.query(SearchContract.URI_FAVORITES, null, FavoritesColumns.ITEMURL + " = ?",
            new String[] { item.getItemUrl() }, null);
    return c;/*w  w  w .j  a va2  s. com*/
}

From source file:Main.java

public static long uriFileSize(Context context, String contentUri) {
    long result = 0;
    Uri uri = Uri.parse(contentUri);/*from w  w  w.j ava2s .co m*/
    if (!uri.getScheme().equals("content")) {
        return (fileUriFileSize(context, contentUri));
    }
    String[] p = { MediaStore.MediaColumns.SIZE };
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
        if (cursor.moveToFirst()) {
            result = cursor.getLong(iColumn);
        }
    }
    return (result);
}

From source file:com.whiuk.philip.opensmime.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {//w  ww. ja  v  a 2s  .  c  om
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(OpenSMIME.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:de.fau.cs.mad.smile.android.encryption.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {/*from  w  w w.  j av a  2  s  .com*/
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(SMileCrypto.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:Main.java

public static String uriTitle(Context context, String contentUri) {
    String result = null;//from   w w w  . j  a  va  2s  . co  m
    String[] p = { MediaStore.MediaColumns.TITLE };
    Uri uri = Uri.parse(contentUri);
    if (!uri.getScheme().equals("content")) {
        return (fileUriTitle(context, contentUri));
    }
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
        if (cursor.moveToFirst()) {
            result = cursor.getString(iColumn);
        }
    }
    return (result);
}

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  .j  a va  2s.c o  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

/**
 * Opens an InputStream for the person's photo and returns the photo as a
 * Bitmap. If the person's photo isn't present returns the
 * placeholderImageResource instead./*w w w.  j ava  2  s . co  m*/
 * 
 * @param context
 *            the Context
 * @param id
 *            the id of the person
 * @param placeholderImageResource
 *            the image resource to use if the person doesn't have a photo
 * @param options
 *            the decoding options, can be set to null
 */
public static Bitmap loadContactPhoto(Context context, Uri contactUri, int placeholderImageResource,
        BitmapFactory.Options options) {

    if (contactUri == null) {
        return loadPlaceholderPhoto(placeholderImageResource, context, options);
    }

    InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);

    Bitmap bm = stream != null ? BitmapFactory.decodeStream(stream, null, options) : null;
    if (bm == null) {
        bm = loadPlaceholderPhoto(placeholderImageResource, context, options);
    }

    return bm;
}

From source file:Main.java

/**
 * Reads <tt>Bitmap</tt> from given <tt>uri</tt> using
 * <tt>ContentResolver</tt>. Output image is scaled to given
 * <tt>reqWidth</tt> and <tt>reqHeight</tt>. Output size is not guaranteed
 * to match exact given values, because only powers of 2 are used as scale
 * factor. Algorithm tries to scale image down as long as the output size
 * stays larger than requested value.//ww w  .j a va2 s  .  c  om
 *
 * @param ctx the context used to create <tt>ContentResolver</tt>.
 * @param uri the <tt>Uri</tt> that points to the image.
 * @param reqWidth requested width.
 * @param reqHeight requested height.
 * @return <tt>Bitmap</tt> from given <tt>uri</tt> retrieved using
 * <tt>ContentResolver</tt> and down sampled as close as possible to match
 * requested width and height.
 * @throws IOException
 */
public static Bitmap scaledBitmapFromContentUri(Context ctx, Uri uri, int reqWidth, int reqHeight)
        throws IOException {
    InputStream imageStream = null;
    try {
        // First decode with inJustDecodeBounds=true to check dimensions
        imageStream = ctx.getContentResolver().openInputStream(uri);
        if (imageStream == null)
            return null;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(imageStream, null, options);
        imageStream.close();

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        imageStream = ctx.getContentResolver().openInputStream(uri);

        return BitmapFactory.decodeStream(imageStream, null, options);
    } finally {
        if (imageStream != null) {
            imageStream.close();
        }
    }
}

From source file:Main.java

public static Bitmap decodeUriToCompressedBitmap(Uri selectedImage, Context context)
        throws FileNotFoundException {
    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;//from  www .ja v  a 2  s.  co m
    BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o);

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2);
}

From source file:Main.java

public static List<String> listAlldir(Context cxt) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Uri uri = intent.getData();//from   w  w w  .  j ava  2s  .  co  m
    List<String> list = new ArrayList<String>();
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = cxt.getContentResolver().query(uri, proj, null, null, null);
    while (cursor.moveToNext()) {
        String path = cursor.getString(0);
        list.add(new File(path).getAbsolutePath());
    }
    return list;
}