2013-04-05 96 views
0

我使用的PhoneGap 2.5.0上,這裏是我如何調用該方法:BitmapFactory.decodeStream(是)失敗在下載文件夾中的一些圖像

try { 
     InputStream is = cordova.getActivity().getContentResolver() 
       .openInputStream(Uri.parse(inputString)); 
     Bitmap bmp = BitmapFactory.decodeStream(is); 
     is.close(); 

代碼工作正常,當我使用拍照相機,但從下載文件夾的一些圖像上隨機失敗。我檢查了這些圖像,它們全部使用像content:// media/external/images/media/xxxx這樣的URL下載。一些文件相當大6MB,而其他文件很小700K。失敗似乎是隨機的,返回null並且不被異常捕獲。

回答

2

doc

解碼輸入流轉換爲位圖。如果輸入流爲空,或者不能用於解碼位圖,則函數返回null。流的位置將是編碼數據讀取後的位置。

因此,無論您的InputStream爲空,或者您打開的文件都無法用於解碼位圖。

3

任何機會它是一個JPEG格式?

看到這個已知問題: -

https://code.google.com/p/android/issues/detail?id=6066

我用解碼位圖如下: -

BitmapFactory.decodeStream(new FlushedInputStream(is), null, opts); 

public class FlushedInputStream extends FilterInputStream { 
public FlushedInputStream(InputStream inputStream) { 
    super(inputStream); 
} 

@Override 
public long skip(long n) throws IOException { 
    long totalBytesSkipped = 0L; 
    while (totalBytesSkipped < n) { 
     long bytesSkipped = in.skip(n - totalBytesSkipped); 
     if (bytesSkipped == 0L) { 
       int myByte = read(); 
       if (myByte < 0) { 
        break; // we reached EOF 
       } else { 
        bytesSkipped = 1; // we read one byte 
       } 
     } 
     totalBytesSkipped += bytesSkipped; 
    } 
    return totalBytesSkipped; 
} 
} 

此外,如果一些圖像都很大,您可能需要設置樣本大小,因此您不會導致太大的分配。

BitmapFactory.Options opts = new BitmapFactory.Options(); 
opts.inSampleSize = sampleSize; 

其中sampleSize是您計算出的合理值。

+0

是的。圖像大多是jpg文件。我使用FlushedInputStream,但似乎遭受同樣的問題。 – Yang 2013-04-05 23:46:41

相關問題