【问题标题】:Android BitmapFactory.decodeStream() return nullAndroid BitmapFactory.decodeStream() 返回 null
【发布时间】:2013-11-05 00:08:06
【问题描述】:

我正在尝试将 InputSteam(视频)转换为位图,但 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 上播放 .mp4 文件?
  • 是的,没错。
  • 即使它有效,性能也可能很糟糕,你想用这个设置实现什么,也许有更好的方法。
  • 我正在尝试将视频绘制到动态壁纸上。

标签: android inputstream bitmapfactory


【解决方案1】:

试试这个

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;
    }   

【讨论】:

  • 函数返回null,outWidth设置为-1,表示解码出错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2015-02-25
  • 2011-05-23
  • 1970-01-01
  • 2017-01-11
  • 2011-01-31
  • 1970-01-01
相关资源
最近更新 更多