2013-11-21 30 views
1

我嘗試從res/raw文件夾(這是一個png資源)解碼位圖(1920x1920)。我需要全尺寸位圖。我想使用BimtapFactory.decodeFileDescriptor而不是BitmapFactory.decodeFile || decodeResource,因爲比其他人處理OOM的可靠性更高: 雖然我嘗試使用此代碼,但位圖爲NULL!但該文件不爲空。我一直在努力玩。 幫助請!爲什麼res/raw文件夾中的位圖是空的?

 Context mCtx=MainActivity.this; 
     Bitmap bm = null;    
     //id is the resId in res/raw 
     AssetFileDescriptor file =mCtx.getResources().openRawResourceFd(R.raw.skin_default_0); 
     bm = BitmapFactory.decodeFileDescriptor(file.getFileDescriptor(), null, 
       options); 

回答

1
InputStream bm = getResources().openRawResource(R.raw.splash); 
    BufferedInputStream bufferedInputStream = new BufferedInputStream(bm); 
    Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream); 
    int nh = (int) (bmp.getHeight() * (512.0/bmp.getWidth())); 
    bmp = Bitmap.createScaledBitmap(bmp, 512, nh, true); 
    view.setImageBitmap(bmp); 

試試這個代碼。

原始文件夾 - 將文件保持爲未壓縮狀態。 我不這麼認爲,與BitmapFactory.decodeStream相比,BitmapFactory.decodeFileDescriptor會給出高效的結果,獲取文件的inputStream並對其進行解碼。如果你認爲圖像太大,並且使用許多圖像,像這樣的應用程序會非常繁重,因爲原始文件夾文件不會被壓縮。如果您想縮放位圖,請使用我用代碼添加的代碼片段代碼。我會給你一個更好的理解。

謝謝...

+0

據說(http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-位圖對象),decodeFileDescriptor調用與decodeStream/decodeFile不同的本地方法。我只是想將方塊(1920 * 1920)的圖像剪成1920 * 1080而沒有OOM。任何想法? – WZY

相關問題