2011-10-20 78 views
3

您好位圖我使用以下代碼來加載從SD卡的圖像,它被正確地運行,加載大圖像在機器人

Bitmap picture=BitmapFactory.decodeFile("/sdcard..."); 

Bitmap picture= BitmapFactory.decodeByteArray(byte[]..); 

byte[]數組包含從SD卡中讀取的字節通過使用FileInputstream並且不爲空。以上兩個代碼都可以正常工作。問題在於,它們不適用於比較大的圖像我有一個1.8 MB的圖像大小。解碼圖像時我的應用程序崩潰。任何用於大圖像的方法都會失敗。 任何解決方案plz thakns。

+0

調整圖像大小然後使用它。 – user370305

+0

可能是什麼原因導致它可能會被解決,因爲我需要使用原始圖像。 – user960971

+0

由於android中堆的大小,並且您的位圖會在堆中分配內存。 – user370305

回答

3

使用下面的代碼來調整圖像大小,你需要的任何大小..

Bitmap picture=BitmapFactory.decodeFile("/sdcard..."); 
    int width = picture.getWidth(); 
    int height = picture.getWidth(); 
    float aspectRatio = (float) width/(float) height; 
    int newWidth = 70; 
    int newHeight = (int) (70/aspectRatio);  
    picture= Bitmap.createScaledBitmap(picture, newWidth, newHeight, true); 
+0

您還可以將圖像加載到內存中,以便不解決問題。 – Stephan

+0

以及問題是,該應用程序崩潰在這個命令...位圖圖片= BitmapFactory.decodeFile(「/ SD卡...」); – user960971

+0

你在真實的設備或模擬器上工作..? –

6

嘗試創建位圖可清除。

byte[] data = ...(read byte array from file) 
    BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inPurgeable = true; 
    Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, opt); 
1

Android的VM具有存儲器的限制,這限制了可解碼的圖像的大小。要在圖像視圖中顯示覆製圖像,可以使用以下代碼。

decode_options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(temp,decode_options); //This will just fill the output parameters 
if(decode_options.outWidth > image_width 
     || decode_options.outHeight > image_height) 
{ 
    float scale_width,scale_height; 

    scale_width = ((float)decode_options.outWidth)/image_width; 
    scale_param = scale_width; 
    scale_height = ((float)decode_options.outHeight)/image_height; 

    if(scale_param < scale_height) 
     scale_param = scale_height; 
} 

decode_options.inJustDecodeBounds = false; 
decode_options.inSampleSize = (int)(scale_param + 1); 
decode_options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
decoded_data = 
     BitmapFactory.decodeFile(temp,decode_options);