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 String getDeviceUUID(Context context) {

    @SuppressWarnings("static-access")
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, tmPhone, androidId;

    tmDevice = "" + tm.getDeviceId();

    tmSerial = "" + tm.getSimSerialNumber();

    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

    String uniqueId = deviceUuid.toString();

    Log.d("debug", "uuid=" + uniqueId);

    return uniqueId;
}

From source file:Main.java

public static String getLatestCameraPicture(Context context) {

    if (!existSDCard()) {
        return null;
    }//from ww w  .j ava2s  .  c  om

    String[] projection = new String[] { MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images.ImageColumns.MIME_TYPE };
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
    if (cursor.moveToFirst()) {
        String path = cursor.getString(1);
        return path;
    }
    return null;
}

From source file:Main.java

/**
 * Return the filename from a uri.// w w w.j ava  2s.co m
 */
public static String getFilename(Context c, Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            return uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Files.FileColumns.DISPLAY_NAME };
            Cursor cursor = c.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.nbos.phonebook.sync.platform.ContactManager.java

public static void setDirtyContacts(Context mContext) { // for testing
    ContentResolver cr = mContext.getContentResolver();
    Uri uri = ContactsContract.RawContacts.CONTENT_URI;
    ContentValues values = new ContentValues();
    values.put(ContactsContract.RawContacts.DIRTY, 1);
    int num = cr.update(uri, values, null, null);
    Log.i("ContactManager", "Resetting " + num + " dirty on contacts");
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/* w w w.  j a v  a2 s  .c om*/
    cursor.close();
    return filePath;
}

From source file:Main.java

public static String checkNull(Context context, int lastImageId, File fileCapture) {
    final String[] imageColumns = { Images.Media._ID, Images.Media.DATA };
    final String imageOrderBy = Images.Media._ID + " DESC";
    final String imageWhere = Images.Media._ID + ">?";
    final String[] imageArguments = { Integer.toString(lastImageId) };

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere,
            imageArguments, imageOrderBy);
    if (cursor == null)
        return null;

    String newpath = null;//from   w w  w .  j  a v  a 2  s  . c  o m
    if (cursor.getCount() >= 2) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
            String data = cursor.getString(cursor.getColumnIndex(Images.Media.DATA));
            if (data.equals(fileCapture.getPath())) {
                int rows = contentResolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media._ID + "=?",
                        new String[] { Long.toString(id) });
                boolean ok = fileCapture.delete();

            } else {
                newpath = data;
            }
        }
    } else {
        newpath = fileCapture.getPath();
        Log.e("MediaUtils", "Not found duplicate.");
    }

    cursor.close();
    return newpath;
}

From source file:io.kristal.appinfos.AppInfosPlugin.java

private synchronized static String getUniqueId(Context context) {
    String uniqueID = null;/*www  . j  a v a 2 s.  c o m*/
    SharedPreferences sharedPrefs = context.getSharedPreferences(PREF_UNIQUE_ID, Context.MODE_PRIVATE);
    uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
    if (uniqueID == null) {
        uniqueID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (uniqueID == null || uniqueID.length() == 0 || "9774d56d682e549c".equals(uniqueID)) {
            // old version of reto meier
            //uniqueID = UUID.randomUUID().toString();
            uniqueID = "35" + //we make this look like a valid IMEI
                    Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
                    + Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
                    + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
                    + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
                    + Build.USER.length() % 10; //13 digits
        }
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString(PREF_UNIQUE_ID, uniqueID);
        editor.commit();
    }
    return uniqueID;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public static DbObject from(Context context, Uri videoUri) throws IOException {
    // Query gallery for camera picture via
    // Android ContentResolver interface
    ContentResolver cr = context.getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;//  ww  w . j av  a2 s  .com
    long videoId = Long.parseLong(videoUri.getLastPathSegment());
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(cr, videoId,
            MediaStore.Video.Thumbnails.MINI_KIND, options);
    int targetSize = 200;
    int width = curThumb.getWidth();
    int height = curThumb.getHeight();
    int cropSize = Math.min(width, height);
    float scaleSize = ((float) targetSize) / cropSize;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleSize, scaleSize);
    curThumb = Bitmap.createBitmap(curThumb, 0, 0, width, height, matrix, true);
    JSONObject base = new JSONObject();
    String localIp = ContentCorral.getLocalIpAddress();
    if (localIp != null) {
        try {
            // TODO: Security breach hack?
            base.put(Contact.ATTR_LAN_IP, localIp);
            base.put(LOCAL_URI, videoUri.toString());
            base.put(MIME_TYPE, cr.getType(videoUri));
        } catch (JSONException e) {
            Log.e(TAG, "impossible json error possible!");
        }
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    curThumb.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte[] data = baos.toByteArray();
    return from(base, data);
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromUriApi19(Context context, Uri uri) {
    String filePath = "";
    String wholeId = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeId.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String selection = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            selection, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/*from w w w  .  j ava 2s .  c  o  m*/
    cursor.close();
    return filePath;
}

From source file:Main.java

public static String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;//from w ww .  j a v a 2 s  . c om
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}