This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Android Video Thumbnail (2)

  • android.provider.MediaStore.Images.Thumbnails
  • android.provider.MediaStore.Video.Thumbnails
  • MediaMetadataRetriever

These three things, but not specifically mentioned, are seen in the ThumbnailUtils source code. In fact, the reason for in-depth understanding comes from: in the project, after uploading the photos and videos we took, when we get the video again, the video is in the server. It is impossible for us to download the video and use it just to get a thumbnail image. So you need to learn how to get video thumbnails in depth.

. In the previous article, we based on the analysis of ThumbnailUtils createVideoThumbnail method source code, see the method of internal use MediaMetadataRetriever object to access video thumbnails, ok! Finally found, turned out to be mediametadaretriever we really get video thumbnail processing class. Let’s take a look at mediametadaretriever.

MediaMetadataRetriever





By studying ThumbnailUtils. CreateVideoThumbnail source MediaMetadataRetriever use method, we can in accordance with the gourd ladle to use MediaMetadataRetriever usage. SetDataSource () sets the absolute path of the data source. It can be a local path or a network URL. Note, however, we should judge the API VERSION, because setDataSource () method in the different API VERSION is different, so should use the android before use. The OS. Build. VERSION. SDK_INT gets the current VERSION of the SDK.

MediaMetadataRetriever media = new MediaMetadataRetriever(); If (SDKVersion >= 14){// Call media.setdatasource (info.srcpath,new HashMap<String, String>()); }else{ media.setDataSource(info.srcPath); } info.bitmap = media.getFrameAtTime();Copy the code

GetFrameAtTime () this method is used to get our thumbnails. We can get our thumbnails at the specified time. Note that it may return null, so we need to determine when we use it.

3. Release () after use.

1. SetDataSource () method

/** * Sets the data source (file pathname) to use. Call this * method before the rest of the methods in this class. This  method may be * time-consuming. * * @param path The path of the input media file. * @throws IllegalArgumentException If  the path is invalid. */ public void setDataSource(String path) throws IllegalArgumentException { FileInputStream is = null; try { is = new FileInputStream(path); FileDescriptor fd = is.getFD(); setDataSource(fd, 0, 0x7ffffffffffffffL); } catch (FileNotFoundException fileEx) { throw new IllegalArgumentException(); } catch (IOException ioEx) { throw new IllegalArgumentException(); } try { if (is ! = null) { is.close(); } } catch (Exception e) {} }Copy the code

SetDataSource (FileDescriptor fd, long offset, long length); Let’s look at the source of this method:

  public native void setDataSource(FileDescriptor fd, long offset, long length)
        throws IllegalArgumentException;
Copy the code

It is found that this method is a native method. For the introduction of Native, you can Google. Similarly, for several other setDataSource methods, fixed native methods are called at the bottom to execute them.

(2) getFrameAtTime source code:

/**
     * Call this method after setDataSource(). This method finds a
     * representative frame at any time position if possible,
     * and returns it as a bitmap. This is useful for generating a thumbnail
     * for an input data source. Call this method if one does not
     * care about where the frame is located; otherwise, please call
     * {@link #getFrameAtTime(long)} or {@link #getFrameAtTime(long, int)}
     *
     * @return A Bitmap containing a representative video frame, which
     *         can be null, if such a frame cannot be retrieved.
     *
     * @see #getFrameAtTime(long)
     * @see #getFrameAtTime(long, int)
     */
    public Bitmap getFrameAtTime() {
        return getFrameAtTime(-1, OPTION_CLOSEST_SYNC);
    }
Copy the code

At the bottom is still call

private native Bitmap _getFrameAtTime(long timeUs, int option);
Copy the code

The basic introduction is so much, let’s do a case to understand, or based on the last article that demo. We only modify the part that gets the video thumbnail.

private void getBitmapFromFile() { Bitmap bitmap = null; Bitmap = bitmapFactory.decodefile (infor.srcpath); bitmap = bitmapFactory.decodefile (infor. }else if(infor.type == 1){ The shooting video / / use ThumnailUtils / / bitmap = ThumbnailUtils createVideoThumbnail (infor. SrcPath, Images. The Thumbnails. MINI_KIND); MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever(); try{ if(android.os.Build.VERSION.SDK_INT >= 14){ metadataRetriever.setDataSource(infor.srcPath, new HashMap<String, String>());; }else{ metadataRetriever.setDataSource(infor.srcPath); } bitmap = metadataRetriever.getFrameAtTime(); infor.bitmap = bitmap; }catch(Exception ex){ ex.printStackTrace(); }finally{ metadataRetriever.release(); }}Copy the code

Let’s test the effect picture:

The basic use of MediaMetadataRetriever is so much, more research, to see the source code!

* android.provider.MediaStore.Images.Thumbnails * android.provider.MediaStore.Video.Thumbnails

So let’s open up our database with SQLiteSpy, and we’ll look at our Thumbnails data table,Baidu entry has a description can see. As shown in figure:

In this table, we see a lot of image information, referring to a document that says’ thumbnails and images are associated with thumbnails. Image_id and images. But I did not find the table of images. I wonder why? Who knows, please leave a message.

Through the above data table, combined with the analysis of ContentProvider, we can basically determine that we can obtain all the pictures in the mobile phone through ContentProvider.

Source code introduction:

public static final String AUTHORITY = "media";

    private static final String CONTENT_AUTHORITY_SLASH = "content://" + AUTHORITY + "/";
Copy the code

This is clearly for our use of ContentProvider services. Moving on, we see that there are multiple static classes defined inside the MediaStore class, including our own:

  • android.provider.MediaStore.Images
  • android.provider.MediaStore.Video

Let’s look at the internal structure of the two static inner classes: Images:

Video:

We found that inside both of them, there was a Thumbnails static class that was the gateway to our query.

So much for the source code! Also not posted, interested in their own SDK to see the source code, we only understand the organizational structure of these classes relationship! Take a look at the class description and member variables through some screenshots. Let’s go through a small example to see how to use it!

The case is still based on the previous demo, this time we add a new media information set

private ArrayList<MeadiaInformation> list;  
Copy the code

Let’s look directly at the code for the query:

Private void getImagesFromDb(){private void getImagesFromDb(){private void getImagesFromDb(){private void getImagesFromDb() getContentResolver(); [] projection = {Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails. Cursor = contentresolver. query(Thumbnails.EXTERNAL_CONTENT_URI, null, null, null); getColumnData(cursor); / / set to listviewAdapter listviewAdapter. AddInformationList (list); } @param cur */ private void getColumnData(cur.moveTofirst ()) {int _id; int image_id; String image_path; int _idColumn = cur.getColumnIndex(Thumbnails._ID); int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID); int dataColumn = cur.getColumnIndex(Thumbnails.DATA); Do {// get id _id = cur.getint (_idColumn); Image_id = cur.getInt(image_idColumn); Image_path = cur.getString(dataColumn); Log.d("Images:", "_id:" + _id + "\n" + "image_id:" + image_id + "\n" + "image_path:"+image_path); Bitmap = bitmapFactory.decodefile (image_path); infor = new MeadiaInformation(); infor.bitmap = bitmap; infor.srcPath = image_path; infor.type = 0; list.add(infor); } while (cur.moveToNext()); }}Copy the code

We call getImagesFromDb() in onCreate to load our program as soon as it comes in. Take a look at the effect picture!

【Android】 Thumbnails

Android thumbnail image acquisition and mapping to the original image