private void buildAlbumInfo(ContentResolver cr) { String[] distinctBucketColumns = new String[]{Media.BUCKET_ID, Media.BUCKET_DISPLAY_NAME}; Cursor bucketCursor = null; try { bucketCursor = cr.query(Media.EXTERNAL_CONTENT_URI, distinctBucketColumns, "0==0)" + " GROUP BY(" + Media.BUCKET_ID, null, Media.DATE_MODIFIED + " desc"); if (bucketCursor != null && bucketCursor.moveToFirst()) { do { String buckId = bucketCursor.getString(bucketCursor.getColumnIndex(Media.BUCKET_ID)); String name = bucketCursor.getString(bucketCursor.getColumnIndex(Media.BUCKET_DISPLAY_NAME)); AlbumEntity album = buildAlbumInfo(name, buckId); if (!TextUtils.isEmpty(buckId)) { buildAlbumCover(cr, buckId, album); } } while (bucketCursor.moveToNext() && !bucketCursor.isLast()); } } finally { if (bucketCursor != null) { bucketCursor.close(); } } }
/** * get the cover and count * * @param buckId album id */ private void buildAlbumCover(ContentResolver cr, String buckId, AlbumEntity album) { String[] photoColumn = new String[]{Media._ID, Media.DATA}; Cursor coverCursor = cr.query(Media.EXTERNAL_CONTENT_URI, photoColumn, SELECTION_ID, new String[]{buckId, "image/jpeg", "image/png", "image/jpg", "image/gif"}, Media.DATE_MODIFIED + " desc"); try { if (coverCursor != null && coverCursor.moveToFirst()) { String picPath = coverCursor.getString(coverCursor.getColumnIndex(Media.DATA)); String id = coverCursor.getString(coverCursor.getColumnIndex(Media._ID)); album.mCount = coverCursor.getCount(); album.mImageList.add(new ImageMedia(id, picPath)); if (album.mImageList.size() > 0) { mBucketMap.put(buckId, album); } } } finally { if (coverCursor != null) { coverCursor.close(); } } }
private void buildAlbumInfo(ContentResolver cr) { String[] distinctBucketColumns = new String[]{Media.BUCKET_ID, Media.BUCKET_DISPLAY_NAME}; Cursor bucketCursor = null; try { bucketCursor = cr.query(Media.EXTERNAL_CONTENT_URI, distinctBucketColumns, "0==0)" + " GROUP BY(" + Media.BUCKET_ID, null, Media.DATE_MODIFIED + " desc"); if (bucketCursor != null && bucketCursor.moveToFirst()) { do { String buckId = bucketCursor.getString(bucketCursor.getColumnIndex(Media.BUCKET_ID)); String name = bucketCursor.getString(bucketCursor.getColumnIndex(Media.BUCKET_DISPLAY_NAME)); AlbumEntity album = buildAlbumInfo(name, buckId); if (!TextUtils.isEmpty(buckId)) { buildAlbumCover(cr, buckId, album); } } while (bucketCursor.moveToNext()); } } finally { if (bucketCursor != null) { bucketCursor.close(); } } }
public PhotoDirectoryLoader(Context context, boolean showGif) { super(context); setProjection(IMAGE_PROJECTION); setUri(Media.EXTERNAL_CONTENT_URI); setSortOrder(Media.DATE_ADDED + " DESC"); setSelection( MIME_TYPE + "=? or " + MIME_TYPE + "=? or "+ MIME_TYPE + "=? " + (showGif ? ("or " + MIME_TYPE + "=?") : "")); String[] selectionArgs; if (showGif) { selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg","image/gif" }; } else { selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg" }; } setSelectionArgs(selectionArgs); }
public void onClick(View v) { int id = v.getId(); if (R.id.btn_share == id) { this.mIvQrCodeArea.setDrawingCacheEnabled(true); PageJumpUtil.jumpToPageSystemShare(this, "", "", "", Uri.parse(Media.insertImage(getContentResolver(), this.mIvQrCodeArea.getDrawingCache(), null, null))); } else if (R.id.btn_refresh == id) { DialogUtil.showDialog(this, getResources().getString(R.string.qrcode_gen_refresh_prompt), "", "", null, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); QrCodeShareActivity.this.refreshQrCode(); } }); } else if (R.id.common_nav_left == id) { finish(); } }
public static String getRealPathByUri(Context context, Uri uri) { if ("file".equals(uri.getScheme())) { return uri.getPath(); } try { Cursor cursor = Media.query(context.getContentResolver(), uri, new String[]{"_data"}); String realPath = null; if (cursor == null) { return null; } int columnIndex = cursor.getColumnIndexOrThrow("_data"); if (cursor.getCount() > 0 && cursor.moveToFirst()) { realPath = cursor.getString(columnIndex); } cursor.close(); return realPath; } catch (Exception e) { return uri.getPath(); } }
public static String getLatestPath(Context context) { String imageLocation = null; Cursor cursor = null; try { cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "_data", "bucket_display_name", "datetaken", "mime_type"}, null, null, "_id DESC"); if (cursor != null && cursor.moveToFirst()) { imageLocation = cursor.getString(cursor.getColumnIndex("_data")); } if (cursor != null) { cursor.close(); } return imageLocation; } catch (Throwable th) { if (cursor != null) { cursor.close(); } } }
static List<Image> getBuckets(Context context) { final Cursor cursor = Media.query(context.getContentResolver(), URI, PROJECTION, "1 = 1) GROUP BY (" + Media.BUCKET_ID, // WTF Media.DATE_ADDED + " DESC"); final List<Image> images = new ArrayList<>(); final File root = Environment.getExternalStorageDirectory(); if (cursor.moveToFirst()) { do { final Image image = Image.fromCursor(cursor); if (image.getParentFile().equals(root)) { continue; } images.add(image); } while (cursor.moveToNext()); } cursor.close(); return images; }
static List<Image> getImages(Context context, String bucketId, int limit) { final Cursor cursor = Media.query(context.getContentResolver(), URI, PROJECTION, Media.BUCKET_ID + " = ?", new String[]{bucketId}, Media.DATE_ADDED + " DESC"); final List<Image> images = new ArrayList<>(); int count = 0; if (cursor.moveToFirst()) { do { images.add(Image.fromCursor(cursor)); count++; } while (count < limit && cursor.moveToNext()); } cursor.close(); return images; }
@Test public void decodeIntentBundle() { Bundle bundle = new Bundle(); ContentUriTrigger uriTrigger = getContentUriTrigger(); Bundle encode = encodeContentUriJob(uriTrigger, coder); bundle.putBundle(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); ArrayList<Uri> uris = new ArrayList<>(); uris.add(ContactsContract.AUTHORITY_URI); uris.add(Media.EXTERNAL_CONTENT_URI); bundle.putParcelableArrayList(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, uris); JobInvocation jobInvocation = coder.decodeIntentBundle(bundle); assertEquals(uris, jobInvocation.getTriggerReason().getTriggeredContentUris()); assertEquals("TAG", jobInvocation.getTag()); assertEquals(uriTrigger.getUris(), ((ContentUriTrigger) jobInvocation.getTrigger()).getUris()); assertEquals(TestJobService.class.getName(), jobInvocation.getService()); assertEquals( RetryStrategy.DEFAULT_EXPONENTIAL.getPolicy(), jobInvocation.getRetryStrategy().getPolicy()); }
@Test public void prepareJob() { Intent intent = new Intent(); Bundle encode = encodeContentUriJob(getContentUriTrigger(), TestUtil.JOB_CODER); intent.putExtra(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode); Parcel container = Parcel.obtain(); container.writeStrongBinder(new Binder()); PendingCallback pcb = new PendingCallback(container); intent.putExtra("callback", pcb); ArrayList<Uri> uris = new ArrayList<>(); uris.add(ContactsContract.AUTHORITY_URI); uris.add(Media.EXTERNAL_CONTENT_URI); intent.putParcelableArrayListExtra(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, uris); JobInvocation jobInvocation = receiver.prepareJob(intent); assertEquals(jobInvocation.getTriggerReason().getTriggeredContentUris(), uris); }
public PhotoDirectoryLoader(Context context, boolean showGif) { super(context); setProjection(IMAGE_PROJECTION); setUri(Media.EXTERNAL_CONTENT_URI); setSortOrder(Media.DATE_ADDED + " DESC"); setSelection( MIME_TYPE + "=? or " + MIME_TYPE + "=? " + (showGif ? ("or " + MIME_TYPE + "=?") : "")); String[] selectionArgs; if (showGif) { selectionArgs = new String[] { "image/jpeg", "image/png", "image/gif" }; } else { selectionArgs = new String[] { "image/jpeg", "image/png" }; } setSelectionArgs(selectionArgs); }
@TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath=null; Uri uri=data.getData(); if(DocumentsContract.isDocumentUri(this,uri)){ String docId=DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1]; String selection=MediaStore.Images.Media._ID+"="+id; imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri=ContentUris.withAppendedId(Uri.parse("cintent://downloads/public_downloads"),Long.valueOf(docId)); imagePath=getImagePath(contentUri,null); } }else if("content".equalsIgnoreCase(uri.getScheme())){ imagePath=getImagePath(uri,null); } displayImage(imagePath); }
/** * 获取最近使用的照片 * * @return */ public List<PhotoModel> getCurrent() { Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, null, null, ImageColumns.DATE_ADDED); if (cursor == null || !cursor.moveToNext()) return new ArrayList<PhotoModel>(); List<PhotoModel> photos = new ArrayList<PhotoModel>(); cursor.moveToLast(); do { if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) { PhotoModel photoModel = new PhotoModel(); photoModel.setOriginalPath(cursor.getString(cursor .getColumnIndex(ImageColumns.DATA))); photos.add(photoModel); } } while (cursor.moveToPrevious()); IOUtils.closeQuietly(cursor); return photos; }
/** * 获取某个相册中的所有图片 * * @param name * @return */ public List<PhotoModel> getAlbum(String name) { Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, "bucket_display_name = ?", new String[] { name }, ImageColumns.DATE_ADDED); if (cursor == null || !cursor.moveToNext()) return new ArrayList<PhotoModel>(); List<PhotoModel> photos = new ArrayList<PhotoModel>(); cursor.moveToLast(); do { if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) { PhotoModel photoModel = new PhotoModel(); photoModel.setOriginalPath(cursor.getString(cursor .getColumnIndex(ImageColumns.DATA))); photos.add(photoModel); } } while (cursor.moveToPrevious()); IOUtils.closeQuietly(cursor); return photos; }
public static final String getRealFilePath(String uriPath, ContentResolver resolver) { String proj[] = {MediaStore.Images.Media.DATA}; Cursor cursor = resolver.query(Uri.parse(uriPath), proj, null, null, null); int index = 0; //TODO ERROR 2 try { index = cursor.getColumnIndexOrThrow(Media.DATA); } catch (IllegalArgumentException e) { return null; } cursor.moveToFirst(); String filePath = cursor.getString(index); cursor.close(); return filePath; }
private boolean isContentSound(int totalCount) { if (mPhotos.size() < Math.min(totalCount, MAX_PHOTO_COUNT)) return false; if (mPhotos.size() == 0) return true; // totalCount is also 0 StringBuilder builder = new StringBuilder(); for (Long imageId : mPhotos) { if (builder.length() > 0) builder.append(","); builder.append(imageId); } Cursor cursor = mContext.getContentResolver().query( CONTENT_URI, COUNT_PROJECTION, String.format("%s in (%s)", Media._ID, builder.toString()), null, null); if (cursor == null) return false; try { Utils.assertTrue(cursor.moveToNext()); return cursor.getInt(0) == mPhotos.size(); } finally { cursor.close(); } }
public PhotoDirectoryLoader(Context context, boolean showGif) { super(context); setProjection(IMAGE_PROJECTION); setUri(Media.EXTERNAL_CONTENT_URI); setSortOrder(Media.DATE_ADDED + " DESC"); setSelection( MIME_TYPE + "=? or " + MIME_TYPE + "=? " + (showGif ? ("or " + MIME_TYPE + "=?") : "")); String[] selectionArgs; if (showGif) { selectionArgs = new String[] { IMAGE_JPEG, IMAGE_PNG, IMAGE_GIF }; } else { selectionArgs = new String[] { IMAGE_JPEG, IMAGE_PNG }; } setSelectionArgs(selectionArgs); }
private void getVideoThumbnail() { String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID }; // 视频其他信息的查询条件 String[] mediaColumns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.DURATION }; Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { do { int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID)); Cursor thumbCursor = cr.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { String thumbnails = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)); // ThumbnailUtils.createVideoThumbnail(thumbnails, // kind); thumbnailList.put("" + id, thumbnails); } } while (cursor.moveToNext()); } cursor.close(); } }
/** 获取最近照片列表 */ public List<PhotoModel> getCurrent() { Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, null, null, ImageColumns.DATE_ADDED); if (cursor == null || !cursor.moveToNext()) return new ArrayList<PhotoModel>(); List<PhotoModel> photos = new ArrayList<PhotoModel>(); cursor.moveToLast(); do { if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) { PhotoModel photoModel = new PhotoModel(); photoModel.setOriginalPath(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA))); photos.add(photoModel); } } while (cursor.moveToPrevious()); return photos; }
/** 获取对应相册下的照片 */ public List<PhotoModel> getAlbum(String name) { Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, "bucket_display_name = ?", new String[] { name }, ImageColumns.DATE_ADDED); if (cursor == null || !cursor.moveToNext()) return new ArrayList<PhotoModel>(); List<PhotoModel> photos = new ArrayList<PhotoModel>(); cursor.moveToLast(); do { if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) { PhotoModel photoModel = new PhotoModel(); photoModel.setOriginalPath(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA))); photos.add(photoModel); } } while (cursor.moveToPrevious()); return photos; }
@Override public void onClick(View view) { upload_Pics_PopupWindow.dismiss(); switch (view.getId()) { case R.id.popup_select_pic_res_camera: getPicByCamera(); break; case R.id.popup_select_pic_res_photograph: Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); break; } }
/** * 获取SD卡中的图片, 在奇巧[19]之前使用此方法, API19后限制了系统应用才有权限使用广播通知系统扫描SD卡 */ private LinkedList<PictureItem> getPictureBeforeKK() { LinkedList<PictureItem> items = null; mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = mContext.getContentResolver(); Cursor mCursor = mContentResolver.query(mImageUri, null, null, null, MediaStore.Images.Media.DATE_ADDED); if (mCursor != null && mCursor.getCount() > 0) { items = new LinkedList<PictureItem>(); mCursor.moveToFirst(); while (mCursor.moveToNext()) { // 获取图片的路径 String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)); // 获取图片的添加到系统的毫秒数 long time = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); PictureItem item = new PictureItem(path, parseTimeToYMD(time)); System.out.println("item = " + item); items.add(item); } mCursor.close(); } return items; }
public HashMap<String, String> getBucketIds() { Uri uri = mBaseUri.buildUpon() .appendQueryParameter("distinct", "true").build(); Cursor cursor = Media.query( mContentResolver, uri, new String[] { Media.BUCKET_DISPLAY_NAME, Media.BUCKET_ID}, whereClause(), whereClauseArgs(), null); try { HashMap<String, String> hash = new HashMap<String, String>(); while (cursor.moveToNext()) { hash.put(cursor.getString(1), cursor.getString(0)); } return hash; } finally { cursor.close(); } }
private Uri getUriFromMediaStore() { ContentValues localContentValues = new ContentValues(); localContentValues.put("mime_type", "image/jpeg"); try { Uri localUri2 = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, localContentValues); return localUri2; } catch (UnsupportedOperationException localUnsupportedOperationException1) { LOG.d("CameraLauncher", "Can't write to external media storage."); try { Uri localUri1 = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, localContentValues); return localUri1; } catch (UnsupportedOperationException localUnsupportedOperationException2) { LOG.d("CameraLauncher", "Can't write to internal media storage."); } } return null; }
private String getRealPathFromURI(Uri contentUri) { Uri uri = null; String photoPath = ""; try { Cursor cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC"); if (cursor != null && cursor.moveToFirst()) { do { uri = contentUri.parse(cursor.getString(cursor .getColumnIndex(Media.DATA))); photoPath = uri.toString(); } while (cursor.moveToNext()); cursor.close(); } } catch (Exception e) { return contentUri.getPath(); } return photoPath; }
@SuppressWarnings("static-access") private String getRealPathFromURI(Uri contentUri) { Uri uri = null; String photoPath = ""; try { Cursor cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC"); if (cursor != null && cursor.moveToFirst()) { do { uri = contentUri.parse(cursor.getString(cursor .getColumnIndex(Media.DATA))); photoPath = uri.toString(); } while (cursor.moveToNext()); cursor.close(); } } catch (Exception e) { return contentUri.getPath(); } return photoPath; }
private boolean getInformationFromMediaDatabase() { String[] fields = { Media.DATA, ImageColumns.ORIENTATION }; Cursor cursor = resolver.query(uri, fields, null, null, null); if (cursor == null) return false; cursor.moveToFirst(); path = cursor.getString(cursor.getColumnIndex(Media.DATA)); int orientation = cursor.getInt(cursor.getColumnIndex(ImageColumns.ORIENTATION)); this.orientation = new Matrix(); this.orientation.setRotate(orientation); cursor.close(); return true; }
private void buildDefaultAlbum(ContentResolver cr) { Cursor cursor = null; try { cursor = cr.query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.BUCKET_ID}, null, null, null); if (cursor != null) { mDefaultAlbum.mCount = cursor.getCount(); } } finally { if (cursor != null) { cursor.close(); } } }