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

public static int getRotationFromGallery(Context context, Uri imageUri) {
    String[] columns = { MediaStore.Images.Media.ORIENTATION };
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null)
        return 0;

    cursor.moveToFirst();/*from w ww  .j a  v a 2  s .  c o m*/

    int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
    return cursor.getInt(orientationColumnIndex);
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentUri) {
    String res = null;/*w w w .java  2 s  .co  m*/
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;/* w w w  . j a  v  a 2s.co  m*/
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

private static ArrayList<String> getNumbers(Context context) {
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = null;/*from w  w  w .  j a  v  a  2s  . c o m*/
    try {
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        cursor.moveToFirst();
        do {
            String phoneNumber = cursor.getString(phoneNumberIdx);
            list.add(phoneNumber);
        } while (cursor.moveToNext());
    } catch (Exception e) {
        Log.d(context.getPackageName(), e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return list;
}

From source file:Main.java

@Nullable
private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException {
    switch (selectedImageURI.getScheme()) {
    case "content":
        return theContext.getContentResolver().openInputStream(selectedImageURI);

    case "file":
        return new FileInputStream(new File(URI.create(selectedImageURI.toString())));

    case "http":
        // Fall through
    case "https":
        final URL url = new URL(selectedImageURI.toString());
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);/*from w w w  .j  a  v a2 s. c o m*/
        connection.connect();
        return connection.getInputStream();

    default:
        Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme());
        return null;
    }
}

From source file:Main.java

public final static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;// w w  w  . java2s  .co m
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static String getPath(Context context, Uri contentUri) {
    Cursor cursor = null;/* w w  w. j  av  a 2  s . c om*/
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static void deleteSMS(Context context, String message, String number) {
    try {// w  w  w.  j  a  va  2  s.  co m

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);

                if (message.equals(body) && address.equals(number)) {

                    context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        // mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());
    }
}

From source file:Main.java

public static String getThumbnailPathForLocalFile(Context context, Uri fileUri) {

    long fileId = getFileId(context, fileUri);
    MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), fileId,
            MediaStore.Video.Thumbnails.MICRO_KIND, null);

    Cursor thumbCursor = null;/* w  ww  . j av  a2s. c o  m*/
    try {
        thumbCursor = context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = " + fileId, null, null);

        if (thumbCursor.moveToFirst()) {
            String thumbPath = thumbCursor
                    .getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
            return thumbPath;
        }

    } finally {
    }

    return null;
}

From source file:Main.java

public static File getRealFileFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;/*from ww w.j a v  a 2s.co m*/
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        return new File(path);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}