Android generates thumbnails for videos, music, and images.

Source: Internet
Author: User
1. Video
For a video, take the first frame as the thumbnail, that is, how to obtain a bitmap object from filepath.

Private bitmap createvideothumbnail (string filepath ){
Bitmap bitmap = NULL;
Mediametadataretriever retriever = new mediametadataretriever ();
Try {
Retriever. setmode (mediametadataretriever. mode_capture_frame_only );
Retriever. setdatasource (filepath );
Bitmap = retriever. captureframe ();
} Catch (illegalargumentexception ex ){
// Assume this is a temporary upt video file
} Catch (runtimeexception ex ){
// Assume this is a temporary upt video file.
} Finally {
Try {
Retriever. Release ();
} Catch (runtimeexception ex ){
// Ignore failures while cleaning up.
}
}
Return bitmap;
} Android provides mediametadataretriever, which is implemented by JNI (media_jni.
Mediametadataretriever has two main functions: mode_get_metadata_only and mode_capture_frame_only.
Set mode to mode_capture_frame_only. Call captureframe to get a frame.
Two other methods can be used:
Extractmetadata extracts file information, including artist, date, year, duration, rating, frame_rate, and video_format.
And extractalbumart extract album information, which can be used in the following music files.

2. Music
For music, use albumimage as a thumbnail or mediametadataretriever

Private bitmap createalbumthumbnail (string filepath ){
Bitmap bitmap = NULL;
Mediametadataretriever retriever = new mediametadataretriever ();
Try {
Retriever. setmode (mediametadataretriever. mode_get_metadata_only );
Retriever. setdatasource (filepath );
Byte [] Art = retriever. extractalbumart ();
Bitmap = bitmapfactory. decodebytearray (art, 0, art. Length );
} Catch (illegalargumentexception ex ){
} Catch (runtimeexception ex ){
} Finally {
Try {
Retriever. Release ();
} Catch (runtimeexception ex ){
// Ignore failures while cleaning up.
}
}
Return bitmap;
}

Retriever. extractalbumart () obtains the byte array. You also need to use bitmapfactory encoding to obtain the bitmap object.

3. Image
The image is simple. Bitmap Bm = NULL;

Options op = new options ();

Op. insamplesize = insamplesize;

Op. injustdecodebounds = false;

Bm = bitmapfactory. decodefile (mfile. getpath (), OP );

Copy the code to directly obtain the bitmap object and narrow down the image to a proper size.
Similarly, for video and music, after retrive to bitmap, it also needs to be reduced.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.