android擷取圖片和視頻的縮圖

來源:互聯網
上載者:User

擷取圖片縮圖:

Java代碼


  1. byte[] imageByte=getImageFromURL(urlPath.trim());
  2. //以下是把圖片轉化為縮圖再載入
  3. BitmapFactory.Options options = new BitmapFactory.Options();
  4. options.inJustDecodeBounds = true;
  5. BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,<span style="background-color: rgb(255, 255, 255);">options </span>); <span style="line-height: 25px; font-size: 14px;
    white-space: normal;"> //此時返回bitmap為空白 </span>
byte[] imageByte=getImageFromURL(urlPath.trim()); //以下是把圖片轉化為縮圖再載入BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options );    //此時返回bitmap為空白 
Java代碼


  1. options.inJustDecodeBounds = false;
  2. int be = (int)(options.outHeight / (float)200);
  3. if (be <= 0){
  4. be = 1;
  5. }
  6. options.inSampleSize = be;
  7. return BitmapFactory.decodeByteArray(imageByte,
    0, imageByte.length,options); //返回縮圖
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);  //返回縮圖

擷取影片縮圖:

/**

* 根據視頻Uri地址取得指定的影片縮圖

* @param cr

* @param uri 本地視頻Uri標示

* @return 返回bitmap類型資料

*/

public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {

Java代碼


  1. Bitmap bitmap = null;
  2. BitmapFactory.Options options = new BitmapFactory.Options();
  3. options.inDither = false;
  4. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  5. Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID },
    null, null,
    null);
  6. if (cursor == null || cursor.getCount() ==
    0) {
  7. return null;
  8. }
  9. cursor.moveToFirst();
  10. String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));
    //image id in image table.s
  11. if (videoId == null) {
  12. return null;
  13. }
  14. cursor.close();
  15. long videoIdLong = Long.parseLong(videoId);
  16. bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
  17. return bitmap;
  18. }
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.sif (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類型資料

*/

Java代碼


  1. public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {
  2. Bitmap bitmap = null;
  3. BitmapFactory.Options options = new BitmapFactory.Options();
  4. options.inDither = false;
  5. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  6. Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID },
    null, null,
    null);
  7. if (cursor == null || cursor.getCount() ==
    0) {
  8. return null;
  9. }
  10. cursor.moveToFirst();
  11. String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));
    //image id in image table.s
  12. if (videoId == null) {
  13. return null;
  14. }
  15. cursor.close();
  16. long videoIdLong = Long.parseLong(videoId);
  17. bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
  18. return bitmap;
  19. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.