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:Main.java

/**
 * Returns The ID for a playlist./*from w  ww. jav  a2 s .c  om*/
 *
 * @param context The {@link Context} to use.
 * @param name The name of the playlist.
 * @return The ID for a playlist.
 */
public static final long getIdForPlaylist(final Context context, final String name) {
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
            new String[] { BaseColumns._ID }, PlaylistsColumns.NAME + "=?", new String[] { name },
            PlaylistsColumns.NAME);
    int id = -1;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            id = cursor.getInt(0);
        }
        cursor.close();
        cursor = null;
    }
    return id;
}

From source file:Main.java

public static Bitmap getBitmapFromUri(Context context, Uri uri) throws FileNotFoundException {
    final InputStream imageStream = context.getContentResolver().openInputStream(uri);
    try {/*from   ww  w.  j  a v a 2s. c o  m*/
        return BitmapFactory.decodeStream(imageStream);
    } finally {
        Closeables.closeQuietly(imageStream);
    }
}

From source file:Main.java

private static Set<Integer> getAllAvailableProtocolVersions(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    Set<Integer> allAvailableVersions = new HashSet<Integer>();
    Uri uri = Uri.parse("content://appsneva.facebook.orca.provider.MessengerPlatformProvider/versions");
    String[] projection = new String[] { "version" };
    Cursor c = contentResolver.query(uri, projection, null, null, null);
    if (c != null) {
        try {//  w ww .ja  v  a  2  s .c  om
            int versionColumnIndex = c.getColumnIndex("version");
            while (c.moveToNext()) {
                int version = c.getInt(versionColumnIndex);
                allAvailableVersions.add(version);
            }
        } finally {
            c.close();
        }
    }
    return allAvailableVersions;
}

From source file:Main.java

public static String getUniqueIdentifier(Context context) {
    if (mUniqueIdentifier.isEmpty())
        mUniqueIdentifier = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);

    return mUniqueIdentifier;
}

From source file:Main.java

private static Set<Integer> getAllAvailableProtocolVersions(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    Set<Integer> allAvailableVersions = new HashSet<Integer>();
    Uri uri = Uri.parse("content://com.facebook.orca.provider.MessengerPlatformProvider/versions");
    String[] projection = new String[] { "version" };
    Cursor c = contentResolver.query(uri, projection, null, null, null);
    if (c != null) {
        try {/*w ww. jav  a2 s . c om*/
            int versionColumnIndex = c.getColumnIndex("version");
            while (c.moveToNext()) {
                int version = c.getInt(versionColumnIndex);
                allAvailableVersions.add(version);
            }
        } finally {
            c.close();
        }
    }
    return allAvailableVersions;
}

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.
 *//*from  w ww .java  2s.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

private static String getFileName(Context context, Uri uri) {
    Log.d("suka", uri.getScheme() + " : " + context.getContentResolver().getType(uri));
    String result = null;/*from   www .j ava2s.com*/
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            cursor.close();
        }
    }
    if (result == null) {
        Log.d("suka", "res " + uri.getPath());
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

public static int getOrientation(Context context, Uri bitmapUri) {
    Cursor cursor = context.getContentResolver().query(bitmapUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor == null || cursor.getCount() != 1) {
        return -1;
    }//w  w w . j  a v  a  2s .c om

    cursor.moveToFirst();
    return cursor.getInt(0);
}

From source file:Main.java

public static Bitmap getBitmapFromUri(@NonNull Context context, @NonNull Uri uri) throws IOException {
    InputStream stream = context.getContentResolver().openInputStream(uri);
    Bitmap rawImage = BitmapFactory.decodeStream(stream);

    try {//from   w  w  w  .j  a v  a  2 s .c  o  m
        stream.close();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return rawImage;
}

From source file:Main.java

/**
 * Reads the data from the {@code column} of the content's {@code queryUri} and returns it as an
 * Array.// ww  w .  ja v a2 s .  c o  m
 */
static private Set<String> getColumnContentAsArray(Context context, Uri queryUri, String column) {
    Cursor cursor = context.getContentResolver().query(queryUri, new String[] { column }, null, null, null);
    Set<String> columnValues = new HashSet<>();
    try {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                columnValues.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return columnValues;
}