本文整理匯總了Java中android.content.Context.getContentResolver方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.getContentResolver方法的具體用法?Java Context.getContentResolver怎麽用?Java Context.getContentResolver使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.content.Context
的用法示例。
在下文中一共展示了Context.getContentResolver方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createPlaylist
import android.content.Context; //導入方法依賴的package包/類
/**
* @param context The {@link Context} to use.
* @param name The name of the new playlist.
* @return A new playlist ID.
*/
public static final long createPlaylist(final Context context, final String name) {
if (name != null && name.length() > 0) {
final ContentResolver resolver = context.getContentResolver();
final String[] projection = new String[]{
PlaylistsColumns.NAME
};
final String selection = PlaylistsColumns.NAME + " = '" + name + "'";
Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
projection, selection, null, null);
if (cursor.getCount() <= 0) {
final ContentValues values = new ContentValues(1);
values.put(PlaylistsColumns.NAME, name);
final Uri uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
values);
return Long.parseLong(uri.getLastPathSegment());
}
if (cursor != null) {
cursor.close();
cursor = null;
}
return -1;
}
return -1;
}
示例2: setDataSource
import android.content.Context; //導入方法依賴的package包/類
public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException,
SecurityException, IllegalStateException {
if (context == null || uri == null)
throw new IllegalArgumentException();
String scheme = uri.getScheme();
if (scheme == null || scheme.equals("file")) {
setDataSource(FileUtils.getPath(uri.toString()));
return;
}
try {
ContentResolver resolver = context.getContentResolver();
mFD = resolver.openAssetFileDescriptor(uri, "r");
if (mFD == null)
return;
setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
return;
} catch (Exception e) {
closeFD();
}
Log.e("Couldn't open file on client side, trying server side %s", uri.toString());
setDataSource(uri.toString());
return;
}
示例3: getSongFromPath
import android.content.Context; //導入方法依賴的package包/類
public static Song getSongFromPath(String songPath, Context context) {
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.DATA;
String[] selectionArgs = {songPath};
String[] projection = new String[]{"_id", "title", "artist", "album", "duration", "track", "artist_id", "album_id"};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
Song song = getSongForCursor(cursor);
cursor.close();
return song;
} else return new Song();
}
示例4: loadBitmap
import android.content.Context; //導入方法依賴的package包/類
/**
* Load a bitmap either from a real file or using the {@link ContentResolver} of the current
* {@link Context} (to read gallery images for example).
*
* Note that, when options.inJustDecodeBounds = true, we actually expect sourceImage to remain
* as null (see https://developer.android.com/training/displaying-bitmaps/load-bitmap.html), so
* getting null sourceImage at the completion of this method is not always worthy of an error.
*/
private static Bitmap loadBitmap(Context context, String imagePath, BitmapFactory.Options options) throws IOException {
Bitmap sourceImage = null;
if (!imagePath.startsWith(CONTENT_PREFIX)) {
try {
Log.d(TAG, "loadBitmap: " + imagePath);
sourceImage = BitmapFactory.decodeFile(imagePath, options);
} catch (Exception e) {
e.printStackTrace();
throw new IOException("Error decoding image file");
}
} else {
ContentResolver cr = context.getContentResolver();
InputStream input = cr.openInputStream(Uri.parse(imagePath));
if (input != null) {
sourceImage = BitmapFactory.decodeStream(input, null, options);
input.close();
}
}
return sourceImage;
}
示例5: purgeRecord
import android.content.Context; //導入方法依賴的package包/類
public void purgeRecord(Context context, String fileName)
{
try
{
contentResolver = context.getContentResolver();
contentUri = DatabaseProvider.TELEPHONE_CALL_CONTENT_URI;
Cursor cursor = contentResolver.query(contentUri, null, TELEPHONE_CALL.RECORD_FILENAME + " = \"" + fileName + "\" AND LOCKED = 0" ,null, TELEPHONE_CALL.DEFAULT_SORT_ORDER);
if (cursor.moveToFirst())
{
String recordFolderPath = cursor.getString(cursor.getColumnIndex(TELEPHONE_CALL.RECORD_PATH));
String recordFileName = cursor.getString(cursor.getColumnIndex(TELEPHONE_CALL.RECORD_FILENAME));
purgeRecordFile(context, recordFolderPath, recordFileName);
contentResolver.delete(contentUri,TELEPHONE_CALL.RECORD_FILENAME + " = \"" + recordFileName + "\"",null);
}
}
catch (Exception e)
{
Log.w("PurgeManager", "purgeRecord : " + context.getString(R.string.log_purge_manager_error_purge_record) + " : " + fileName + " : " + e);
databaseManager.insertLog(context, "" + context.getString(R.string.log_purge_manager_error_purge_record) + " : " + fileName, new Date().getTime(), 2, false);
}
}
示例6: getImgInfo
import android.content.Context; //導入方法依賴的package包/類
/**
* 獲取本地圖片信息
* @param context
* @param map
*/
public static void getImgInfo(Context context, HashMap<String, ArrayList<String>> map) {
//獲取圖片信息表
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = context.getContentResolver();
Cursor mCursor = mContentResolver.query(imageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_TAKEN + " DESC");
while (mCursor.moveToNext()) {
String imgPath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
//獲取圖片父路徑
String parentPath = new File(imgPath).getParentFile().getName();
if (!map.containsKey(parentPath)) {
ArrayList<String> childList = new ArrayList<String>();
childList.add(imgPath);
map.put(parentPath, childList);
} else {
map.get(parentPath).add(imgPath);
}
}
mCursor.close();
}
示例7: onReceive
import android.content.Context; //導入方法依賴的package包/類
@Override
public void onReceive(Context context, Intent intent) {
final ContentResolver resolver = context.getContentResolver();
final String action = intent.getAction();
if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(action)) {
resolver.call(RecentsProvider.buildRecent(), RecentsProvider.METHOD_PURGE, null, null);
} else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equals(action)) {
final Uri data = intent.getData();
if (data != null) {
final String packageName = data.getSchemeSpecificPart();
resolver.call(RecentsProvider.buildRecent(), RecentsProvider.METHOD_PURGE_PACKAGE,
packageName, null);
}
}
}
示例8: countAppsForRepo
import android.content.Context; //導入方法依賴的package包/類
public static int countAppsForRepo(Context context, long repoId) {
ContentResolver resolver = context.getContentResolver();
final String[] projection = {Schema.ApkTable.Cols._COUNT_DISTINCT};
Uri apkUri = ApkProvider.getRepoUri(repoId);
Cursor cursor = resolver.query(apkUri, projection, null, null, null);
int count = 0;
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
count = cursor.getInt(0);
}
cursor.close();
}
return count;
}
示例9: getDirItemsNumber
import android.content.Context; //導入方法依賴的package包/類
private static int getDirItemsNumber(Context ctx, Uri u)
{
int itemsNumber = 0;
Cursor c = null;
try
{
ContentResolver cr = ctx.getContentResolver();
String dirId = DocumentsContract.getDocumentId(u);
Uri dirUri = DocumentsContract.buildChildDocumentsUriUsingTree(u, dirId);
c = cr.query(dirUri, projection, null, null, null);
if (c != null)
{
itemsNumber = c.getCount();
}
}
catch (Exception e)
{
// notning to do;
}
finally
{
if (c != null)
{
c.close();
}
}
return itemsNumber;
}
示例10: hasAccessGranted
import android.content.Context; //導入方法依賴的package包/類
public static boolean hasAccessGranted(Context CONTEXT) {
ContentResolver contentResolver = CONTEXT.getContentResolver();
String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = CONTEXT.getPackageName();
// check to see if the enabledNotificationListeners String contains our package name
return !(enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName));
}
示例11: renamePlaylist
import android.content.Context; //導入方法依賴的package包/類
/**
* Rename Playlist
*
* @param context
* @param newplaylist
* @param playlist_id
*/
public static void renamePlaylist(Context context, String newplaylist, long playlist_id) {
if (permissionManager.writeExternalStorageGranted(context)) {
Uri newuri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
String where = MediaStore.Audio.Playlists._ID + " =? ";
String[] whereVal = {Long.toString(playlist_id)};
values.put(MediaStore.Audio.Playlists.NAME, newplaylist);
resolver.update(newuri, values, where, whereVal);
} else {
Log.e("PlaylistHelper", "Permission failed");
}
}
示例12: testLoadResource_withNullFileDescriptor_callsLoadFailed
import android.content.Context; //導入方法依賴的package包/類
@Test
public void testLoadResource_withNullFileDescriptor_callsLoadFailed() {
Context context = RuntimeEnvironment.application;
Uri uri = Uri.parse("file://nothing");
ContentResolver contentResolver = context.getContentResolver();
ContentResolverShadow shadow = Shadow.extract(contentResolver);
shadow.registerFileDescriptor(uri, null /*fileDescriptor*/);
FileDescriptorLocalUriFetcher fetcher =
new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri);
fetcher.loadData(Priority.NORMAL, callback);
verify(callback).onLoadFailed(isA(FileNotFoundException.class));
}
示例13: updateItemsInDatabaseHelper
import android.content.Context; //導入方法依賴的package包/類
static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
final ArrayList<ItemInfo> items, final String callingFunction) {
final ContentResolver cr = context.getContentResolver();
final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Runnable r = new Runnable() {
public void run() {
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
int count = items.size();
for (int i = 0; i < count; i++) {
ItemInfo item = items.get(i);
final long itemId = item.id;
final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
ContentValues values = valuesList.get(i);
ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
updateItemArrays(item, itemId, stackTrace);
}
try {
cr.applyBatch(LauncherProvider.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
}
};
runOnWorkerThread(r);
}
示例14: deleteItemsFromDatabase
import android.content.Context; //導入方法依賴的package包/類
/**
* Removes the specified items from the database
*/
static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
final ContentResolver cr = context.getContentResolver();
Runnable r = new Runnable() {
public void run() {
for (ItemInfo item : items) {
final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
cr.delete(uri, null, null);
// Lock on mBgLock *after* the db operation
synchronized (sBgLock) {
switch (item.itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
sBgFolders.remove(item.id);
for (ItemInfo info: sBgItemsIdMap) {
if (info.container == item.id) {
// We are deleting a folder which still contains items that
// think they are contained by that folder.
String msg = "deleting a folder (" + item + ") which still " +
"contains items (" + info + ")";
Log.e(TAG, msg);
}
}
sBgWorkspaceItems.remove(item);
break;
case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
(ShortcutInfo) item));
// Fall through.
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
sBgWorkspaceItems.remove(item);
break;
case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
break;
}
sBgItemsIdMap.remove(item.id);
}
}
}
};
runOnWorkerThread(r);
}
示例15: get
import android.content.Context; //導入方法依賴的package包/類
/**
* Find by the content URI of a repo ({@link RepoProvider#getContentUri(long)}).
*/
public static Repo get(Context context, Uri uri) {
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, Cols.ALL, null, null, null);
return cursorToRepo(cursor);
}