2012-11-05 24 views
2

可能重複:
「Bitmap too large to be uploaded into a texture」位圖到大

我拍照用的相機,那麼我保存在SD卡中的圖片,然後使用

Bitmap bm = BitmapFactory.decodeFile(path);

要獲得位圖,然後

imageView.setImageBitmap(bm);來設置它。

但是,當我把它改成使用

mFrameLayout.addView(imageView);我的觀點是沒有圖像顯示和我回去Bitmap to large to be uploaded into texture

位圖是1944 high, 2592 wide

有什麼想法?

我正在使用平板電腦,10.1英寸,acer iconia,a500,運行ics。

+0

你檢查了這個帖子:http://stackoverflow.com/questions/10271020/bitmap-too-large-to-be-uploaded-into-a-texture? –

+0

這是一個常見錯誤,目前android只支持最高2000px,ü幸運的是不會因爲出錯導致崩潰... –

+0

http://stackoverflow.com/questions/12108841/how-to-catch -error-bitmap-too-large-to-be-upload-into-a-texture – Krishnabhadra

回答

6

嘗試用這種

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ 
     try { 
      //Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

      //The new size we want to scale to 
      final int REQUIRED_WIDTH=WIDTH; 
      final int REQUIRED_HIGHT=HIGHT; 
      //Find the correct scale value. It should be the power of 2. 
      int scale=1; 
      while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) 
       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

我現在就試一試。 – FabianCook

+1

爲什麼你將o.outWidth和o.outHeight除以2?它不應該只是o.outWidth/scale和o.outHeight/scale嗎? –

2

使用這個類的圖像視圖應用之前給你的位圖縮小到一個較小的所需的大小。

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 


class BitmapLoader 
{ 
    public static int getScale(int originalWidth,int originalHeight, 
    final int requiredWidth,final int requiredHeight) 
    { 
     //a scale of 1 means the original dimensions 
     //of the image are maintained 
     int scale=1; 

//calculate scale only if the height or width of 
//the image exceeds the required value. 
if((originalWidth>requiredWidth) || (originalHeight>requiredHeight)) 
{ 
    //calculate scale with respect to 
    //the smaller dimension 
    if(originalWidth<originalHeight) 
     scale=Math.round((float)originalWidth/requiredWidth); 
    else 
     scale=Math.round((float)originalHeight/requiredHeight); 

} 

return scale; 
} 

public static BitmapFactory.Options getOptions(String filePath, 
    int requiredWidth,int requiredHeight) 
{ 

BitmapFactory.Options options=new BitmapFactory.Options(); 
//setting inJustDecodeBounds to true 
//ensures that we are able to measure 
//the dimensions of the image,without 
//actually allocating it memory 
options.inJustDecodeBounds=true; 

//decode the file for measurement 
BitmapFactory.decodeFile(filePath,options); 

//obtain the inSampleSize for loading a 
//scaled down version of the image. 
//options.outWidth and options.outHeight 
//are the measured dimensions of the 
//original image 
options.inSampleSize=getScale(options.outWidth, 
     options.outHeight, requiredWidth, requiredHeight); 

//set inJustDecodeBounds to false again 
//so that we can now actually allocate the 
//bitmap some memory 
options.inJustDecodeBounds=false; 

return options; 

} 



public static Bitmap loadBitmap(String filePath, 
    int requiredWidth,int requiredHeight){ 


BitmapFactory.Options options= getOptions(filePath, 
     requiredWidth, requiredHeight); 

return BitmapFactory.decodeFile(filePath,options); 
} 
} 
從活動

然後,調用這個類提供從SD卡獲取位圖的文件路徑,並設置requiredWidth和requiredHeight的尺寸

Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight) 

方法要縮放位圖。現在使用reqBitmap。