@Override public Bitmap getBitmapLogo(Context context) { Bitmap thumbnailBitmap = null; if (android.os.Build.VERSION.SDK_INT >= 5) { Cursor cursor = context .getContentResolver() .query( android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, android.provider.MediaStore.Video.Media.DATA + " = '" + file.getAbsolutePath() + "'", null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Video.Media._ID)); cursor.close(); Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail( context.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); thumbnailBitmap = bitmap; } else { thumbnailBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_video); } } else { thumbnailBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_video); } return thumbnailBitmap; }
/*获取缩略图*/ 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(); } } }
@Override public void setViewImage(ImageView v, String s) { /* * String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA, * MediaStore.Video.Thumbnails.VIDEO_ID }; * * Cursor thumbCursor = managedQuery( * MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, * MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); */ // Log.d(TAG, "We have " + s); String[] mediaColumns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE }; Cursor thumb_cursor = managedQuery( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, MediaStore.Video.Media.DATA + " = ? ", new String[] {s}, null); if (thumb_cursor.moveToFirst()) { // Found entry for video via file path. Now we have its // video_id, we can check for a cache thumbnail or request to // generate one. // XXX Check cache, and load from filepath, using code above. /* Log .d( TAG, " We have id " + thumb_cursor .getLong(thumb_cursor .getColumnIndexOrThrow(MediaStore.Video.Media._ID))); */ Bitmap bm = MediaStore.Video.Thumbnails.getThumbnail( getContentResolver(), thumb_cursor.getLong( thumb_cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)), MediaStore.Video.Thumbnails.MICRO_KIND, null); if (bm != null && v != null) { v.setImageBitmap(bm); } } else { // set default icon v.setImageResource(R.drawable.icon); } }
/** * Loads music data. This method may take long, so be sure to call it asynchronously without * blocking the main thread. */ public void prepare() { /// Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri uri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI; Log.i(TAG, "Querying media..."); Log.i(TAG, "URI: " + uri.toString()); // Perform a query on the content resolver. The URI we're passing specifies that we // want to query for all audio media on external storage (e.g. SD card) Cursor cur = mContentResolver.query( uri, null, null, // MediaStore.Video.Media.IS_MUSIC + " = 1", null, null); Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor.")); if (cur == null) { // Query failed... Log.e(TAG, "Failed to retrieve music: cursor is null :-("); return; } if (!cur.moveToFirst()) { // Nothing to query. There is no music on the device. How boring. Log.e(TAG, "Failed to move cursor to first row (no query results)."); return; } Log.i(TAG, "Listing..."); // retrieve the indices of the columns where the ID, title, etc. of the song are int artistColumn = cur.getColumnIndex(MediaStore.Video.Media.ARTIST); int titleColumn = cur.getColumnIndex(MediaStore.Video.Media.TITLE); int albumColumn = cur.getColumnIndex(MediaStore.Video.Media.ALBUM); int durationColumn = cur.getColumnIndex(MediaStore.Video.Media.DURATION); int idColumn = cur.getColumnIndex(MediaStore.Video.Media._ID); int dataColumnIndex = cur.getColumnIndex(MediaStore.Video.Media.DATA); Log.i(TAG, "Title column index: " + String.valueOf(titleColumn)); Log.i(TAG, "ID column index: " + String.valueOf(titleColumn)); // add each song to mItems do { Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn)); Bitmap thumbnail = MediaStore.Video.Thumbnails.getThumbnail( mContentResolver, cur.getLong(idColumn), MediaStore.Video.Thumbnails.MICRO_KIND, null); mItems.add( new Item( thumbnail, cur.getString(dataColumnIndex), cur.getLong(idColumn), cur.getString(artistColumn), cur.getString(titleColumn), cur.getString(albumColumn), cur.getLong(durationColumn))); } while (cur.moveToNext()); Log.i(TAG, "Done querying media. MediaRetriever is ready."); }
private Bitmap getThumbnail(long id) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inScaled = true; options.inJustDecodeBounds = false; options.inPurgeable = true; options.inInputShareable = true; ContentResolver cr = mContext.getContentResolver(); bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null); return bitmap; }
/** get videos form {@link MediaStore.Video.Media.EXTERNAL_CONTENT_URI} */ public List<MediaInfo> getVideoInfo() { List<MediaInfo> list = new ArrayList<MediaInfo>(); ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query( DreamConstant.VIDEO_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER); if (null == cursor) { } else { if (cursor.moveToFirst()) { MediaInfo mediaInfo = null; do { mediaInfo = new MediaInfo(); long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID)); long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)); // 时长 long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE)); // 文件大小 String url = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)); // 文件路径 String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)); if (new File(url).exists()) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; // get video thumbail Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail( contentResolver, id, Images.Thumbnails.MICRO_KIND, options); mediaInfo.setId(id); mediaInfo.setDuration(duration); mediaInfo.setSize(size); mediaInfo.setUrl(url); mediaInfo.setDisplayName(displayName); mediaInfo.setIcon(bitmap); list.add(mediaInfo); } } while (cursor.moveToNext()); } cursor.close(); } return list; }
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) { } }
/** * 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; }