2011-10-20 45 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);