2013-12-17 22 views
-3

當我加載圖像形成SD卡,我得到異常java.lang.OutOfMemoryError同時顯示在圖庫中的圖片

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 

這裏是我的代碼:

public class Images extends Activity implements OnItemLongClickListener { 
     private Uri[] mUrls; 
     String[] mFiles = null; 
     ImageView selectImage; 
     Gallery g; 
     static final String MEDIA_PATH = new String("/mnt/sdcard/DCIM/Camera/"); 

     @Override 
     public void onCreate(Bundle icicle) { 

      super.onCreate(icicle); 
      setContentView(R.layout.main); 
      selectImage = (ImageView) findViewById(R.id.image); 
      g = (Gallery) findViewById(R.id.gallery); 
      File images = new File(MEDIA_PATH); 
      Log.i("files", images.getAbsolutePath()); 

      File[] imagelist = images.listFiles(new FilenameFilter() { 
       @Override 
       public boolean accept(File dir, String name) { 
        return ((name.endsWith(".jpg")) || (name.endsWith(".png"))); 
       } 
      }); 
      Log.i("files", imagelist.toString()); 
      String[] mFiles = null; 
      mFiles = new String[imagelist.length]; 

      for (int i = 0; i < imagelist.length; i++) { 
       mFiles[i] = imagelist[i].getAbsolutePath(); 
      } 
      System.out.println(mFiles.length); 

      mUrls = new Uri[mFiles.length]; 
      System.out.println(mUrls.length); 
      for (int i = 0; i < mFiles.length; i++) { 
       mUrls[i] = Uri.parse(mFiles[i]); 
      } 

      g.setAdapter(new ImageAdapter(this)); 


    //  g.setOnItemSelectedListener(this); 
      g.setOnItemLongClickListener(this); 



     } 

     public class ImageAdapter extends BaseAdapter { 

      // int mGalleryItemBackground; 

      public ImageAdapter(Context c) { 
       mContext = c; 
      } 

      public int getCount() { 
       return mUrls.length; 
      } 

      public Object getItem(int position) { 
       return position; 
      } 

      public long getItemId(int position) { 
       return position; 
      } 

      public View getView(int position, View convertView, ViewGroup parent) { 
       Log.i("ok5", "ok"); 
       ImageView i = new ImageView(mContext); 

       i.setImageURI(mUrls[position]); 
       Log.i("ok", "ok"); 
       i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
       i.setLayoutParams(new Gallery.LayoutParams(100, 100)); 
       return i; 
      } 

      private Context mContext; 
     } 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 

      selectImage.setImageURI(mUrls[arg2]); 
      System.out.println("path: "+mUrls[arg2]); 

      Uri f = mUrls[arg2]; 
      File f1 = new File(f.toString()); 
      System.out.println("f1: "+f1); 
      return false; 
     } 
+0

解碼您的圖像路徑使用BitmapFactory.Options – Piyush

+0

我的答案幫助..? –

回答

0

當你加載大位圖文件中,BitmapFactory類提供了幾種解碼方法(decodeByteArray(),decodeFile(),decodeResource()等)。

STEP 1

的inJustDecodeBounds屬性設置爲true,而解碼避免了內存分配,爲位圖對象返回空但設置outWidth,outHeight和outMimeType。該技術允許您在構建位圖之前讀取圖像數據的尺寸和類型(以及內存分配)。

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
String imageType = options.outMimeType; 
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it. 

STEP 2

告訴解碼器子樣本圖像,加載一個較小的版本到內存中,inSampleSize設置爲true,你BitmapFactory.Options對象。

例如,分辨率爲2048x1536且用inSampleSize爲4解碼的圖像會生成大約512x384的位圖。將其加載到內存中時,整個圖像使用0.75MB而不是12MB。

這裏的計算樣本大小值是兩個基於目標寬度和高度的功率的方法:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
    int reqWidth, int reqHeight) { 

// First decode with inJustDecodeBounds=true to check dimensions 
final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(res, resId, options); 

// Calculate inSampleSize 
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
return BitmapFactory.decodeResource(res, resId, options); 
} 

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
// Raw height and width of image 
final int height = options.outHeight; 
final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 

    final int halfHeight = height/2; 
    final int halfWidth = width/2; 

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
    // height and width larger than the requested height and width. 
    while ((halfHeight/inSampleSize) > reqHeight 
      && (halfWidth/inSampleSize) > reqWidth) { 
     inSampleSize *= 2; 
    } 
} 

return inSampleSize; 
} 

請仔細閱讀本鏈接瞭解詳情。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html