Android BitmapFactory.decodeStream()返回null

时间:2013-11-05 00:08:06

标签: android inputstream bitmapfactory

我正在尝试将InputSteam(视频)转换为Bitmap,但decodeStream()返回null。

代码示例:

InputStream is = getResources().openRawResource(R.drawable.test1);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap surface = BitmapFactory.decodeStream(is,null,options);
//surface is null

是因为输入蒸汽太大了吗?如果是这样,我将如何修改输入流只读取1 1920x1080帧? 这需要非常快,我尝试使用MediaMetadataRetriever但它太慢了。大局是我试图将.mp4绘制到Canvas。

1 个答案:

答案 0 :(得分:0)

试试这个

Bitmap surface = decodeFile(new File("file path here"));

    private Bitmap decodeFile(File f){

      int IMGAE_REZ  = 100;

        try {

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){

                if(width_tmp/2<IMGAE_REZ || height_tmp/2<IMGAE_REZ)
                break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }
            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}

        return null;
    }