android获取图片和视频的缩略图

获取图片缩略图:
?
代码片段,双击复制
01
02
03
04
05
byte [] imageByte=getImageFromURL(urlPath.trim());
                        //以下是把图片转化为缩略图再加载
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true ;
                        BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length,<SPAN style= "BACKGROUND-COLOR: #ffffff" >options </SPAN>);   <SPAN style= "LINE-HEIGHT: 25px; WHITE-SPACE: normal; FONT-SIZE: 14px" >&nbsp; //此时返回bitmap为空&nbsp;</SPAN>


?
代码片段,双击复制
01
02
03
04
05
06
07
                        options.inJustDecodeBounds = false ;
                        int be = ( int )(options.outHeight / ( float ) 200 );
                 if (be <= 0 ){
                        be = 1 ;
                 }
                 options.inSampleSize = be;     
                        return BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length,options);   //返回缩略图


获取视频缩略图:
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 
        * 根据视频Uri地址取得指定的视频缩略图
        * @param cr  
        * @param uri  本地视频Uri标示
        * <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 返回bitmap类型数据
        */
 
        public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {
                                Bitmap bitmap = null ;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inDither = false ;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Cursor cursor = cr.query(uri, new String[] { MediaStore.Video.Media._ID }, null , null , null );
         
                if (cursor == null || cursor.getCount() == 0 ) {
                     return null ;
                }
                cursor.moveToFirst();
                String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));   //image id in image table.s
         
                if (videoId == null ) {
                return null ;
                }
                cursor.close();
                long videoIdLong = Long.parseLong(videoId);
                bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
 
                return bitmap;
                }

/** 
        * 根据视频在手机中的地址路径取得指定的视频缩略图
        * @param cr  
        * @param fileName  本地视频地址
        * @return 返回bitmap类型数据

        */

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {
                Bitmap bitmap = null ;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inDither = false ;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Cursor cursor = cr.query(uri, new String[] { MediaStore.Video.Media._ID }, null , null , null );
         
                if (cursor == null || cursor.getCount() == 0 ) {
                     return null ;
                }
                cursor.moveToFirst();
                String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));   //image id in image table.s
 
                if (videoId == null ) {
                return null ;
                }
                cursor.close();
                long videoIdLong = Long.parseLong(videoId);
                bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
 
                return bitmap;
                }
        
转自:http://www.eoeandroid.com/thread-234926-1-1.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值