1. 程式人生 > >【GT-安卓應用開發之Glide獲取視訊縮圖】

【GT-安卓應用開發之Glide獲取視訊縮圖】

前言:期待已久的週末終於到來了,雖然明天還需要上班,但是今天卻不像平日裡迫切的趕回家。飯後,閒來無事結合最近的一個專案需求,編寫一個小demo,主要是實現視訊檔案縮圖的獲取。

        我的思路是,首先獲取所有的視訊檔案列表,然後依次獲取縮圖並展示。但是出於時間因素,在這裡我只獲取第一個視訊檔案的縮圖,也就是這個demo的流程大致為“獲取視訊列表—獲取第一個視訊的縮圖展示—儲存縮圖至本地”。

        關鍵程式碼:

1、利用Glide獲取視訊縮圖

Glide.with(this).load(video.getUrl1()).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(target);

2、獲取視訊列表

public List<Video> getVideos() {
    List<Video> list = null;
    if (this != null) {
        Cursor cursor = getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                null, null);
        if (cursor != null) {
            list = new ArrayList<Video>();
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                String title = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                String album = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
                String artist = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                String displayName = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                String mimeType = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                String path = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                long duration = cursor
                        .getInt(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                long size = cursor
                        .getLong(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                Video video = new Video();
                video.setName1(title);
                video.setSize1(size);
                video.setUrl1(path);
                video.setDuration1(duration);
                list.add(video);
            }
            cursor.close();
        }
    }
    return list;
}

3、儲存圖片

public  String savePhoto(Bitmap photoBitmap,String photoName) {
    String path = "";
    String localPath = null;
    path  = Environment.getExternalStorageDirectory()+"/"+photoName+".png";
    File photoFile = new File(path);
    if (photoFile.exists()){
        photoFile.mkdirs();
    }
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(photoFile);
        if (photoBitmap != null) {
            if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,
                    fileOutputStream)) {
                localPath = photoFile.getPath();
                fileOutputStream.flush();
                Toast.makeText(MainActivity.this,"圖片成功儲存至:"+localPath,Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(MainActivity.this,"Bitmap為空",Toast.LENGTH_SHORT).show();
        }
    } catch (FileNotFoundException e) {
        photoFile.delete();
        localPath = null;
        e.printStackTrace();
        Log.e("saveerror","FileNotFound");
    } catch (IOException e) {
        photoFile.delete();
        localPath = null;
        Log.e("saveerror",e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
                fileOutputStream = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return localPath;
}

注意事項:

由於Glide是非同步載入,如果採用ImageView截圖的方式來獲得縮圖,可能會出現空指標的問題,為了解決這一問題,Glide的into物件並不是ImageView,而是自定義的一個SimpleTarget,在onResourceReady中處理bitmap並且儲存