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

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.//from w  w w. j  a va2 s . co m
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static void insertUrl(Context context, Uri contentUri, URL sourceUrl) {
    if (DEBUG) {//  w w  w . j  av  a  2s.c  o m
        Log.d(TAG, "Inserting " + sourceUrl + " to " + contentUri);
    }
    InputStream is = null;
    OutputStream os = null;
    try {
        is = sourceUrl.openStream();
        os = context.getContentResolver().openOutputStream(contentUri);
        copy(is, os);
    } catch (IOException ioe) {
        Log.e(TAG, "Failed to write " + sourceUrl + "  to " + contentUri, ioe);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // Ignore exception.
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                // Ignore exception.
            }
        }
    }
}

From source file:Main.java

private static String fileUriTitle(Context context, String contentUri) {
    String result = null;/*from w  ww .ja va 2  s.co m*/
    String[] p = { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE };
    Uri uri = Uri.parse(contentUri);
    String path = uri.getPath();
    String last = Uri.parse(path).getLastPathSegment();
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, p, // which columns
            MediaStore.MediaColumns.DISPLAY_NAME + "='" + last + "'", // which rows
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int tcol = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
        if (cursor.moveToFirst()) {
            result = cursor.getString(tcol);
        }
    }
    return (result);
}

From source file:Main.java

/**
 * Gets binary from uri./*  w ww  .j  a v a  2 s .  c o  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:ee.ria.DigiDoc.util.FileUtils.java

private static File cacheUriAsDataFile(Context context, Uri uri, String fileName) {
    File directory = getDataFilesCacheDirectory(context);
    return writeUriDataToDirectory(directory, uri, fileName, context.getContentResolver());
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

private static File uriAsContainerFile(Context context, Uri uri, String fileName) {
    File directory = getContainerCacheDirectory(context);
    return writeUriDataToDirectory(directory, uri, fileName, context.getContentResolver());
}

From source file:Main.java

/**
 * Update content.// www. j a v a2s .c o  m
 *
 * @param context
 *            context
 * @param sourceUri
 *            source uri
 * @param file
 *            output file
 * @return source uri
 */
public static Uri updateContent(Context context, Uri sourceUri, File file) {
    long now = System.currentTimeMillis() / MILLISEC_PER_SEC;
    final ContentValues values = new ContentValues();
    values.put(Images.Media.DATE_MODIFIED, now);
    values.put(Images.Media.DATE_ADDED, now);
    values.put(Images.Media.SIZE, file.length());
    context.getContentResolver().update(sourceUri, values, null, null);
    return sourceUri;
}

From source file:Main.java

public static String getLocalAddress(Context context) {
    // http://stackoverflow.com/a/33749218
    // http://stackoverflow.com/a/34494459

    String address = "";

    if (Build.VERSION.SDK_INT <= 22) {
        address = BluetoothAdapter.getDefaultAdapter().getAddress();
    } else {/*from  w  w w. j  av  a 2s  . c  o  m*/
        address = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
    }

    return address;
}

From source file:can.yrt.onebusaway.backup.Backup.java

/**
 * Performs a restore from the SD card.//from  w w  w .  j  a  va2s  . c om
 *
 * @param context
 */
public static void restore(Context context) throws IOException {
    File dbPath = getDB(context);
    File backupPath = getBackup(context);

    // At least here we can decide that the database is closed.
    ContentProviderClient client = null;
    try {
        client = context.getContentResolver().acquireContentProviderClient(ObaContract.AUTHORITY);
        ObaProvider provider = (ObaProvider) client.getLocalContentProvider();
        provider.closeDB();

        FileUtils.copyFile(backupPath, dbPath);

    } finally {
        if (client != null) {
            client.release();
        }
    }
}

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

public static void deleteFavorites(Context context, ListItem item) {
    final AsyncQueryHandler dbHandler = new AsyncQueryHandler(context.getContentResolver()) {
    };/*  www. j ava2  s .c om*/
    dbHandler.startDelete(0, null, SearchContract.URI_FAVORITES, FavoritesColumns.ITEMURL + " = ?",
            new String[] { item.getItemUrl() });
}