Android Video Acquisition and image thumbnails

Source: Internet
Author: User

1. There are two ways to get a video Thumbnail (1) through the content provider (2) create a thumbnail manually

(1) The disadvantage is that you must update the media library to see the thumbnail of the latest video.

[Java]

Copy codeThe Code is as follows :/**
* @ Param context
* @ Param cr
* @ Param Videopath
* @ Return
*/
Public static Bitmap getVideoThumbnail (Context context, ContentResolver cr, String Videopath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Video. Media. DATA, MediaStore. Video. Media. _ ID ,};
String whereClause = MediaStore. Video. Media. DATA + "= '" + Videopath + "'";
Cursor cursor = testcr. query (MediaStore. Video. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String videoPath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){

Int _ idColumn = cursor. getColumnIndex (MediaStore. Video. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Video. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
VideoPath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Video. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}

/**
* @ Param context
* @ Param cr
* @ Param Videopath
* @ Return
*/
Public static Bitmap getVideoThumbnail (Context context, ContentResolver cr, String Videopath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Video. Media. DATA, MediaStore. Video. Media. _ ID ,};
String whereClause = MediaStore. Video. Media. DATA + "= '" + Videopath + "'";
Cursor cursor = testcr. query (MediaStore. Video. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String videoPath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){

Int _ idColumn = cursor. getColumnIndex (MediaStore. Video. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Video. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
VideoPath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Video. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
} (2) it takes a little time to create a thumbnail manually.

[Java]Copy codeThe Code is as follows :/**
* Getting video thumbnails
* @ Param videoPath
* @ Param width
* @ Param height
* @ Param kind
* @ Return
*/
Private Bitmap getVideoThumbnail (String videoPath, int width, int height, int kind ){
Bitmap bitmap = null;
Bitmap = ThumbnailUtils. createVideoThumbnail (videoPath, kind );
Bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT );
Return bitmap;
}

/**
* Getting video thumbnails
* @ Param videoPath
* @ Param width
* @ Param height
* @ Param kind
* @ Return
*/
Private Bitmap getVideoThumbnail (String videoPath, int width, int height, int kind ){
Bitmap bitmap = null;
Bitmap = ThumbnailUtils. createVideoThumbnail (videoPath, kind );
Bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT );
Return bitmap;
}

2. Image thumbnails

[Java]

Copy codeThe Code is as follows :/**
*
* @ Param context
* @ Param cr
* @ Param Imagepath
* @ Return
*/
Public static Bitmap getImageThumbnail (Context context, ContentResolver cr, String Imagepath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Images. Media. DATA, MediaStore. Images. Media. _ ID ,};
String whereClause = MediaStore. Images. Media. DATA + "= '" + Imagepath + "'";
Cursor cursor = testcr. query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String imagePath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){

Int _ idColumn = cursor. getColumnIndex (MediaStore. Images. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Images. Media. DATA );

Do {
_ Id = cursor. getInt (_ idColumn );
ImagePath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Images. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}

/**
*
* @ Param context
* @ Param cr
* @ Param Imagepath
* @ Return
*/
Public static Bitmap getImageThumbnail (Context context, ContentResolver cr, String Imagepath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Images. Media. DATA, MediaStore. Images. Media. _ ID ,};
String whereClause = MediaStore. Images. Media. DATA + "= '" + Imagepath + "'";
Cursor cursor = testcr. query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String imagePath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){

Int _ idColumn = cursor. getColumnIndex (MediaStore. Images. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Images. Media. DATA );

Do {
_ Id = cursor. getInt (_ idColumn );
ImagePath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Images. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}

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.