如何使用方形毕加索库加载视频缩略图?

时间:2014-06-04 13:05:34

标签: android picasso

目前,我正在使用picasso将MediaStore图像缩略图加载到ListView,并带有以下代码段:( video.getData()返回图像的实际路径,例如mnt/sdcard/...

Picasso.with(this.context)
       .load(new File(photo.getData()))
       .resize(50, 50).config(config)
       .centerCrop()
       .into(viewHolder.imageViewItem);

现在我只是传递video.getData()而不是photo.getData()而无法加载MediaStore视频缩略图?

4 个答案:

答案 0 :(得分:36)

首先您需要创建VideoRequestHandler

public class VideoRequestHandler extends RequestHandler{
    public String SCHEME_VIDEO="video";
    @Override
    public boolean canHandleRequest(Request data) 
    {
        String scheme = data.uri.getScheme();
        return (SCHEME_VIDEO.equals(scheme));
    }

    @Override
    public Result load(Request data, int arg1) throws IOException 
    {
         Bitmap bm = ThumbnailUtils.createVideoThumbnail(data.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND);
         return new Result(bm,LoadedFrom.DISK);  
    }
}

之后

 VideoRequestHandler videoRequestHandler;
 Picasso picassoInstance;

仅构建一次

 videoRequestHandler = new VideoRequestHandler();
 picassoInstance = new Picasso.Builder(context.getApplicationContext())
  .addRequestHandler(videoRequestHandler)
  .build();

然后从路径

加载文件
 picassoInstance.load(VideoRequestHandler.SCHEME_VIDEO+":"+filepath).into(holder.videoThumbnailView);

答案 1 :(得分:0)

之前,您有两种选择:

1)事先打电话,例如检索视频并存储位图:

Bitmap thumbnailBitmap = MediaStore.Video.Thumbnails.getThumbnail(content, id, MediaStore.Video.Thumbnails.MINI_KIND, options);

2)创建自定义下载程序(未经本地媒体验证)

但现在,使用Picasso 2.4,您可以创建一个RequestHandler。您可以查看此页面,例如:http://blog.jpardogo.com/requesthandler-api-for-picasso-library/

在我目前的用法中,我获取缩略图路径,然后使用该路径调用Picasso。为了获得缩略图路径,我提到了:android get video thumbnail PATH, not Bitmap

答案 2 :(得分:0)

/* @flow */

import React from 'react'
import Separator from './components/Separator'
import {Button, StyleSheet, TextInput, View} from 'react-native'

const styles = StyleSheet.create({
    subject: {
        height: 20,
    },
    body: {
        height: 100,
    },
})

export default class NewQuestion extends React.Component {
    static navigationOptions = ({navigation}) => ({
        title: 'AIBU',
        headerLeft: (
            <Button
                onPress={() => {
                    navigation.goBack()
                }}
                title="Back"
            />
        ),
    })

    state = {
        subject: '',
        body: '',
    }

    render() {
        return (
            <View>
                <TextInput
                    autoFocus
                    onChangeText={subject => this.setState({subject})}
                    placeholder="Enter your AIBU subject..."
                    style={styles.subject}
                    value={this.state.subject}
                />
                <Separator />
                <TextInput
                    multiline
                    onChangeText={body => this.setState({body})}
                    placeholder="Enter your AIBU description..."
                    style={styles.body}
                    value={this.state.body}
                />
            </View>
        )
    }
}

答案 3 :(得分:0)

注意-如果仅想在图像视图中显示缩略图,并且不对其应用多余的毕加索功能,请遵循此注释。 我刚刚对其进行了一些修改,以显示圆形缩略图,看起来很酷。

请勿使用Picasso。这是一种非常简单的方法。

videoPathUrl = /*your Video File Url */
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(videoPathUrl , MediaStore.Video.Thumbnails.MICRO_KIND);
bMap = transformBitmapToCircularBitmap(bMap);
yourImageView.setImageBitmap(bMap);

和transformBitmapToCircularBitmap函数如下-

public static Bitmap transformBitmapToCircularBitmap(Bitmap source)
{
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap,
            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}