android 獲取視頻,圖片縮略圖的具體實(shí)現(xiàn)
1、獲取視頻縮略圖有兩個(gè)方法(1)通過(guò)內(nèi)容提供器來(lái)獲取(2)人為創(chuàng)建縮略圖
(1)缺點(diǎn)就是必須更新媒體庫(kù)才能看到最新的視頻的縮略圖
[java]
/**
* @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)人為創(chuàng)建縮略圖要耗費(fèi)一點(diǎn)時(shí)間
[java]
/**
* 獲取視頻縮略圖
* @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;
}
/**
* 獲取視頻縮略圖
* @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、圖片縮略圖
[java]
/**
*
* @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;
}
相關(guān)文章
Android仿eleme點(diǎn)餐頁(yè)面二級(jí)聯(lián)動(dòng)列表
本站一直在點(diǎn)外賣(mài),于是心血來(lái)潮就像仿餓了么做個(gè)站,接下來(lái)通過(guò)本文給大家介紹android 二級(jí)聯(lián)動(dòng)列表,仿eleme點(diǎn)餐頁(yè)面的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解
這篇文章主要介紹了Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤(pán)
本文主要介紹了Android設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤(pán)的實(shí)現(xiàn)代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
Android實(shí)現(xiàn)Service重啟的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Service重啟的方法,涉及Android操作Service組件實(shí)現(xiàn)服務(wù)重啟的功能,需要的朋友可以參考下2015-05-05
Android RecyclerView網(wǎng)格布局示例解析
這篇文章主要介紹了Android RecyclerView網(wǎng)格布局示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
實(shí)現(xiàn)activity管理器一次退出所有activity
退出所有Activity網(wǎng)上有很多很多種說(shuō)法,推薦的一種方法是自定義一個(gè)Activity管理器,來(lái)管理所有已打開(kāi)的Activity,要退出的時(shí)候再通過(guò)這個(gè)管理器來(lái)退出所有Activity,下面是一個(gè)簡(jiǎn)單的Activity管理器代碼2014-01-01
android bitmap compress(圖片壓縮)代碼
android bitmap compress(圖片壓縮)代碼,需要的朋友可以參考一下2013-06-06
Android編程實(shí)現(xiàn)WebView全屏播放的方法(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)WebView全屏播放的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)WebView全屏播放的布局與功能相關(guān)技巧,需要的朋友可以參考下2015-11-11
Android 跨進(jìn)程模擬按鍵(KeyEvent )實(shí)例詳解
這篇文章主要介紹了Android 跨進(jìn)程模擬按鍵(KeyEvent )實(shí)例詳解的相關(guān)資料,類(lèi)似手機(jī)遙控器的需求就可以這么做,需要的朋友可以參考下2016-11-11

