查询图片库数据
Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = PhotoAlbumActivity.this.getContentResolver();
Cursor cursor = resolver.query(imgUri, null,
MediaStore.Images.Media.DATA + " like ? and (" +
MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?)",
new String[]{"%" + PathUtil.getPhotoPath() + "/%",
"image/jpeg", "image/png"},
MediaStore.Images.Media.DATE_MODIFIED + " DESC");
获取相关信息
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media
.DATA));
Long time = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media
.DATE_MODIFIED));
Date date = new Date(time * 1000);
扫描图片加入数据库
public static void refresh(Context context, String filename){//filename是我们的文件全名,包括后缀哦
MediaScannerConnection.scanFile(context,
new String[] { filename }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
删除图片
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=?",
new String[] { imgPath }, null);
boolean result = false;
if (cursor.moveToFirst()) {
long id = cursor.getLong(0);
Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uri = ContentUris.withAppendedId(contentUri, id);
int count = resolver.delete(uri, null, null);
result = count == 1;
} else {
File file = new File(imgPath);
result = file.delete();
}
网友评论