2011-02-06 68 views

回答

5

此代碼將做的工作:

Bitmap getPreview(URI uri) { 
    File image = new File(uri); 

    BitmapFactory.Options bounds = new BitmapFactory.Options(); 
    bounds.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(image.getPath(), bounds); 
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 
     return null; 

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight 
      : bounds.outWidth; 

    BitmapFactory.Options opts = new BitmapFactory.Options(); 
    opts.inSampleSize = originalSize/THUMBNAIL_SIZE; 
    return BitmapFactory.decodeFile(image.getPath(), opts);  
} 

您可能要計算的2最近的電源用於inSampleSize,因爲it's said要快。

+4

什麼是THUMBNAIL SIZE – 2013-10-17 04:35:56

+1

構造函數File(Uri)未定義 – hasanghaforian 2015-04-22 16:49:40

2

我相信這段代碼生成縮略圖從文件SD卡上的最快的方法:

public static Bitmap decodeFile(String file, int size) { 
    //Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(file, o); 

    //Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = (int)Maths.pow(2, (double)(scale-1)); 
    while (true) { 
     if (width_tmp/2 < size || height_tmp/2 < size) { 
      break; 
     } 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale++; 
    } 

    //Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    return BitmapFactory.decodeFile(file, o2); 
} 
9

您可以在API使用ThumnailUtil級Java的

Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height); 


public static Bitmap createVideoThumbnail (String filePath, int kind) 

新增簡單地創建縮略圖視頻和圖像級別8 爲視頻創建視頻縮略圖。如果視頻損壞或格式不被支持,可能會返回null。

參數 文件路徑視頻文件的路徑 那種可以MINI_KIND或MICRO_KIND

For more Source code of Thumbnail Util class

Developer.android.com

0

一些嘗試我無法從SD獲取圖像的縮略圖路徑。 我解決了這個問題,得到一個android圖像位圖,然後在適配器中爲gridview創建圖像視圖(或者你需要的地方)。因此,我調用方法imageView.setImageBitmap(someMethod(Context context, imageID))

Bitmap someMethod(Context context, long imageId){ 

    Bitmap bitmap = Media.Images.Thumbnails.getThumbnail(context.getAplicationContext.getContentResolver(), imageid, MediaStore.Images.Thumbnails.MINI_KIND, null); 
    return bitmap; 
} 

您可以使用本指南(Get list of photo galleries on Android

0

如果你喜歡HQ縮略圖從SD卡獲取圖像ID,所以用[RapidDecoder] [1]庫。這是簡單如下:

import rapid.decoder.BitmapDecoder; 
... 
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image) 
          .scale(width, height) 
          .useBuiltInDecoder(true) 
          .decode(); 

不要忘了,如果你想縮小小於50%和HQ結果使用內置的解碼器。 我在API等級8中測試過它:)

相關問題