android 擷取視頻,圖片縮圖的具體實現

來源:互聯網
上載者:User

1、擷取影片縮圖有兩個方法(1)通過內容提供器來擷取(2)人為建立縮圖

(1)缺點就是必須更新媒體庫才能看到最新的視頻的縮圖

[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)人為建立縮圖要耗費一點時間

[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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.