2014-01-05 3 views
5

私はユニバーサルイメージローダー(https://github.com/nostra13/Android-Universal-Image-Loader)を使ってグリッドビューでビデオサムネイルを表示することを試しています。画像サムネイルを問題なく表示できるようになりました。Universal Image Loaderを使用してビデオサムネイルを表示することはできますか?

public class MediaCursorAdapter extends SimpleCursorAdapter implements Filterable { 
    @Override 
    public void bindView(View rowView, Context context, Cursor cursor) { 
     String contentUri = getContentUri(cursor); 

     ImageView imgThumb = (ImageView) rowView.findViewById(R.id.imgThumb); 

     ImageLoader.getInstance().displayImage(contentUri, imgThumb); 
    } 
} 

いくつかのコードを簡単にするために省略されている:私はサムネイルを表示するためにそれを使用する方法

@Override 
public void onCreate() { 
    super.onCreate(); 
    initUil(); 
} 

private void initUil() { 
    DisplayImageOptions displayOptions = new DisplayImageOptions.Builder() 
      .cacheInMemory(true) 
      .cacheOnDisc(true) 
      .build(); 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) 
      .taskExecutor(ThreadPool.getExecutorService()) 
      .defaultDisplayImageOptions(displayOptions) 
      .build(); 

    ImageLoader.getInstance().init(config); 
} 

:私はApplicationクラスにUILを初期化する方法

contentUriは画像URIまたは動画URIのどちらでもかまいませんが、いずれの場合もcontent://...

このライブラリを使用してビデオサムネイルを表示できますか?どうやって?

答えて

10

[OK]を、私はそれを考え出しました。 ImageLoaderConfigurationには画像デコーダを渡すオプションがあります。それはinfo.getImageKey()はなく、と、オリジナルのURIは、この画像に対して指定返すことを言うUILのマニュアルでは

public class SmartUriDecoder implements ImageDecoder { 
    private final ContentResolver m_contentResolver; 
    private final BaseImageDecoder m_imageUriDecoder; 

    public SmartUriDecoder(ContentResolver contentResolver, BaseImageDecoder imageUriDecoder) { 
     if (imageUriDecoder == null) { 
      throw new NullPointerException("Image decoder can't be null"); 
     } 

     m_contentResolver = contentResolver; 
     m_imageUriDecoder = imageUriDecoder; 
    } 

    @Override 
    public Bitmap decode(ImageDecodingInfo info) throws IOException { 
     if (TextUtils.isEmpty(info.getImageKey())) { 
      return null; 
     } 

     String cleanedUriString = cleanUriString(info.getImageKey()); 
     Uri uri = Uri.parse(cleanedUriString); 
     if (isVideoUri(uri)) { 
      return makeVideoThumbnail(info.getTargetSize().getWidth(), info.getTargetSize().getHeight(), getVideoFilePath(uri)); 
     } 
     else { 
      return m_imageUriDecoder.decode(info); 
     } 
    } 

    private Bitmap makeVideoThumbnail(int width, int height, String filePath) { 
     if (filePath == null) { 
      return null; 
     } 
     Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND); 
     Bitmap scaledThumb = scaleBitmap(thumbnail, width, height); 
     thumbnail.recycle(); 
     return scaledThumb; 
    } 

    private boolean isVideoUri(Uri uri) { 
     String mimeType = m_contentResolver.getType(uri); 
     return mimeType.startsWith("video/"); 
    } 

    private String getVideoFilePath(Uri uri) { 
     String columnName = MediaStore.Video.VideoColumns.DATA; 
     Cursor cursor = m_contentResolver.query(uri, new String[] { columnName }, null, null, null); 
     try { 
      int dataIndex = cursor.getColumnIndex(columnName); 
      if (dataIndex != -1 && cursor.moveToFirst()) { 
       return cursor.getString(dataIndex); 
      } 
     } 
     finally { 
      cursor.close(); 
     } 
     return null; 
    } 

    private Bitmap scaleBitmap(Bitmap origBitmap, int width, int height) { 
     float scale = Math.min(
       ((float)width)/((float)origBitmap.getWidth()), 
       ((float)height)/((float)origBitmap.getHeight()) 
     ); 
     return Bitmap.createScaledBitmap(origBitmap, 
       (int)(((float)origBitmap.getWidth()) * scale), 
       (int)(((float)origBitmap.getHeight()) * scale), 
       false 
     ); 
    } 

    private String cleanUriString(String contentUriWithAppendedSize) { 
     // replace the size at the end of the URI with an empty string. 
     // the URI will be in the form "content://....._256x256 
     return contentUriWithAppendedSize.replaceFirst("_\\d+x\\d+$", ""); 
    } 
} 

ImageDecoder smartUriDecoder = new SmartUriDecoder(getContentResolver(), new BaseImageDecoder(false)); 

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) 
      .taskExecutor(ThreadPool.getExecutorService()) 
      .defaultDisplayImageOptions(displayOptions) 
      .imageDecoder(smartUriDecoder) 
      .build(); 

そしてSmartUriDecoderクラス:ここ

は、私は初期設定を変更する方法です最後に追加サイズがあり、元のURIを取得する方法が見つかりませんでした。したがって、cleanUriString()のコードの匂いの理由。

+0

ヘッドアップ、私はこのアプローチを試しましたが、メディアアイテムの束のためにサムネイルを生成したときに大きなスローダウンに気付きました。それは、UILがデータディレクトリ内のビデオ自体のためのキャッシュエントリを作成しているように見えます。これは、多くの処理時間と大きなファイル(50 MBファイルなどを参照)を意味します。私はまだそれを見ていますが、vidsとuilを統合する大きなスタートに感謝します。 – Core

+0

私もこれに気付きましたが、それは初めて初めて起こります。動画のサムネイルが 'MediaStore'になかったので、' ThumbnailUtils.createVideoThumbnail() 'を呼び出すと、すべての動画サムネイルが強制的に作成されたと考えました。しかし、私はその方法がどのように機能しているかについては分かりませんので、私はここで完全に間違っているかもしれません。 –

+0

こんにちは、私はあなたの仕事に基づいて私のカスタムImageDecoderを作ろうとしましたが、私はそれを使用しようとすると、デコーダはこのエラーで失敗します:E/ImageLoader:null java.lang.NullPointerException?任意の手掛かりはなぜですか? – mass441

0

私はこの人が解決したように見えますが、ユニバーサルイメージローダーを使用していないようです。

Android: Is it possible to display video thumbnails?

+0

私はビデオサムネイルを生成する方法を知っていますが、キャッシングと同期の設定可能なソリューションを探しています.UILは完璧な候補のようです。 –

関連する問題