Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android獲取SD卡上圖片和視頻縮略圖的小例子

Android獲取SD卡上圖片和視頻縮略圖的小例子

編輯:關於Android編程

      如何判斷文件呢? 可以通過Cursor遍歷數據庫,對比INTERNAL_CONTENT_URI字段的值,這是一個Uri,這裡保存著Android手機SD卡上的多媒體文件完整路徑。

[java]
復制代碼 代碼如下:
Uri originalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                //若為視頻則為MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
                ContentResolver cr = this.getContentResolver();
                Cursor cursor = cr.query(originalUri, null, null, null, null);
                if (cursor == null) {
                    return;
                }
                for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()) {
                    long thumbNailsId = cursor.getLong(cursor   .getColumnIndex("_ID"));
                    Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr,
                            thumbNailsId, Video.Thumbnails.MICRO_KIND, null);
                    //若為視頻則為 
//                  Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, 
//                          thumbNailsId, Video.Thumbnails.MICRO_KIND, null); 
                    return bitmap;
                }

Uri originalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    //若為視頻則為MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    ContentResolver cr = this.getContentResolver();
    Cursor cursor = cr.query(originalUri, null, null, null, null);
    if (cursor == null) {
     return;
    }
    for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()) {
     long thumbNailsId = cursor.getLong(cursor .getColumnIndex("_ID"));
     Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr,
       thumbNailsId, Video.Thumbnails.MICRO_KIND, null);
     //若為視頻則為
//     Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr,
//       thumbNailsId, Video.Thumbnails.MICRO_KIND, null);
     return bitmap;
    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved