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.