2013-11-05 27 views
0

我想將InputSteam(視頻)轉換爲位圖,但decodeStream()返回null。Android BitmapFactory.decodeStream()return 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文件繪製到畫布上。

+0

你嘗試播放的MP4到畫布? – Kai

+0

是的,這是正確的。 –

+0

即使它有效,性能可能會非常糟糕,你試圖用這種設置實現什麼,也許有更好的方法。 – Kai

回答

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

函數返回null,outWidth被設置爲-1,表示解碼時出錯。 –

相關問題