public void checkForNewImages() { // Here we'll only check for newer images final String[] columns = {MediaStore.Images.Thumbnails._ID}; final String orderBy = MediaStore.Images.Media._ID; Cursor imagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, MediaStore.Images.Media._ID + " > " + lastId, null, orderBy); int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); int count = imagecursor.getCount(); for (int i = 0; i < count; i++) { imagecursor.moveToPosition(i); int id = imagecursor.getInt(image_column_index); ImageItem imageItem = new ImageItem(); imageItem.id = id; lastId = id; imageItem.img = MediaStore.Images.Thumbnails.getThumbnail( getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); imageItem.selection = true; // newly added item will be selected by default images.add(imageItem); } imagecursor.close(); notifyDataSetChanged(); }
/*获取缩略图*/ public Bitmap getThumbnail( ContentResolver contentResolver, long originalId, int kind, BitmapFactory.Options options, boolean isVideo) { Thread thread = Thread.currentThread(); ThreadStatus threadStatus = getOrCreateThreadStatus(thread); if (!canThreadDecoding(thread)) { return null; } try { synchronized (threadStatus) { threadStatus.mThumbRequesting = true; } if (isVideo) { return MediaStore.Video.Thumbnails.getThumbnail( contentResolver, originalId, thread.getId(), kind, options); } else { return MediaStore.Images.Thumbnails.getThumbnail( contentResolver, originalId, thread.getId(), kind, options); } } finally { synchronized (threadStatus) { threadStatus.mThumbRequesting = false; threadStatus.notifyAll(); } } }
public void initialize() { images.clear(); final String[] columns = {MediaStore.Images.Thumbnails._ID}; final String orderBy = MediaStore.Images.Media._ID; Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy); if (imagecursor != null) { int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); int count = imagecursor.getCount(); for (int i = 0; i < count; i++) { imagecursor.moveToPosition(i); int id = imagecursor.getInt(image_column_index); ImageItem imageItem = new ImageItem(); imageItem.id = id; lastId = id; imageItem.img = MediaStore.Images.Thumbnails.getThumbnail( getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); images.add(imageItem); } imagecursor.close(); } notifyDataSetChanged(); }
public String insertImage(ContentResolver cr, Bitmap source, String title, String description) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, title); values.put(MediaStore.Images.Media.DISPLAY_NAME, title); values.put(MediaStore.Images.Media.DESCRIPTION, description); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // Add the date meta data to ensure the image is added at the front of the gallery values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis()); values.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis()); values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); if (frontFacingCamera && orientation == 90) // weird flipped front-facing camera { orientation += 180; } values.put(MediaStore.Images.Media.ORIENTATION, orientation); Uri url = null; String stringUrl = null; /* value to be returned */ try { url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); if (source != null) { OutputStream imageOut = cr.openOutputStream(url); try { source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut); } finally { imageOut.close(); } long id = ContentUris.parseId(url); // Wait until MINI_KIND thumbnail is generated. Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail( cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); // This is for backward compatibility. storeThumbnail(cr, miniThumb, id, 50F, 50F, MediaStore.Images.Thumbnails.MICRO_KIND); } else { cr.delete(url, null, null); url = null; } } catch (Exception e) { if (url != null) { cr.delete(url, null, null); url = null; } } if (url != null) { stringUrl = url.toString(); } return stringUrl; }
@Override public Bitmap getBitmap(int position) { // id 가져오기 long id = getItemId(position); // Bitmap 샘플링 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; // 2의 배수 // id 로부터 bitmap 생성 return MediaStore.Images.Thumbnails.getThumbnail( mContext.getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, options); }
public synchronized void cancelThreadDecoding(Thread thread, ContentResolver contentResolver) { ThreadStatus threadStatus = getOrCreateThreadStatus(thread); threadStatus.mState = State.CANCEL; if (threadStatus.mOptions != null) { threadStatus.mOptions.requestCancelDecode(); } notifyAll(); try { synchronized (threadStatus) { while (threadStatus.mThumbRequesting) { MediaStore.Images.Thumbnails.cancelThumbnailRequest(contentResolver, -1, thread.getId()); MediaStore.Video.Thumbnails.cancelThumbnailRequest(contentResolver, -1, thread.getId()); threadStatus.wait(200); } } } catch (Exception exception) { } }
public Bitmap getBitmap() throws OutOfMemoryError { if (mBitmap != null) { return mBitmap; } mBitmap = MediaStore.Images.Thumbnails.getThumbnail( mContext.getContentResolver(), mId, MediaStore.Images.Thumbnails.MINI_KIND, sBitmapOptionsCache); String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; Cursor cur = mContext .getContentResolver() .query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, orientationColumn, MediaStore.Images.Media._ID + "=?", new String[] {Long.toString(mId)}, null); int orientation = -1; if (cur != null) { if (cur.moveToFirst()) { orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); } cur.close(); } if (orientation != 0 && orientation != -1) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); if (mBitmap != null) { int height = mBitmap.getHeight(); int width = mBitmap.getWidth(); mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true); } } return mBitmap; }
/** * 获取图片缩略图 只有Android2.1以上版本支持 * * @param imgName * @param kind MediaStore.Images.Thumbnails.MICRO_KIND * @return */ protected Bitmap loadImgThumbnail(String imgName, int kind) { Bitmap bitmap = null; String[] proj = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME}; Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DISPLAY_NAME + "='" + imgName + "'", null, null); if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { ContentResolver crThumb = getContentResolver(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; bitmap = MediaStore.Images.Thumbnails.getThumbnail(crThumb, cursor.getInt(0), kind, options); } return bitmap; }
protected Bitmap getThumbnailOfLastPhoto() { Cursor cursor = MediaStore.Images.Media.query( getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN }, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT 1"); Bitmap thumb = null; if (cursor.moveToNext()) { int id = cursor.getInt(0); thumb = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); } cursor.close(); return thumb; }
/** * Get a micro (96x96) thumbnail from the image at the Uri. The method is a mix of solutions from * this thread on SO: * http://stackoverflow.com/questions/5548645/get-thumbnail-uri-path-of-the-image-stored-in-sd-card-android * * @param uri Uri to the image * @return Bitmap or null */ private Bitmap getThumbnail(Uri uri) { String[] projection = {MediaStore.Images.Media._ID}; String result = null; Cursor cursor = managedQuery(uri, projection, null, null, null); Log.d(TAG, "cursor: " + cursor); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); Log.d(TAG, "column: " + column_index); cursor.moveToFirst(); long imageId = cursor.getLong(column_index); // cursor.close(); // can't close or else it crashes on a second attempt at changing pic Log.d(TAG, "imageID: " + imageId); Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), imageId, MediaStore.Images.Thumbnails.MICRO_KIND, null); Log.d(TAG, "bitmap: " + bitmap); return bitmap; }
/** * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This should not be called * on the UI thread. * * @param context * @param uri * @param mimeType * @return * @author paulburke */ public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) { if (DEBUG) Log.d(TAG, "Attempting to get thumbnail"); if (!isMediaUri(uri)) { Log.e(TAG, "You can only retrieve thumbnails for images and videos."); return null; } Bitmap bm = null; if (uri != null) { final ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; try { cursor = resolver.query(uri, null, null, null, null); if (cursor.moveToFirst()) { final int id = cursor.getInt(0); if (DEBUG) Log.d(TAG, "Got thumb ID: " + id); if (mimeType.contains("video")) { bm = MediaStore.Video.Thumbnails.getThumbnail( resolver, id, MediaStore.Video.Thumbnails.MINI_KIND, null); } else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) { bm = MediaStore.Images.Thumbnails.getThumbnail( resolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null); } } } catch (Exception e) { if (DEBUG) Log.e(TAG, "getThumbnail", e); } finally { if (cursor != null) cursor.close(); } } return bm; }
@TargetApi(7) public static Bitmap getThumbnail(ContentResolver cr, long origId, int kind, Options options) { return MediaStore.Images.Thumbnails.getThumbnail(cr, origId, kind, options); }