using the following code, you should check OutOfMemory Error
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) try { // We need to recyle unused bitmaps if (Main_bitmap != null) { Main_bitmap.recycle(); } Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); if (cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds=true; BitmapFactory.decodeFile(filePath,bmpFactoryOptions); if ( checkBitmapFitsInMemory(bmpFactoryOptions.outWidth,bmpFactoryOptions.outHeight, 2)) { BitmapFactory.Options options4 = new BitmapFactory.Options(); options4.inSampleSize = 1; Main_bitmap = BitmapFactory.decodeFile(filePath, options4); iv_set_image.setImageBitmap(Main_bitmap); } else { Toast.makeText(this,"java.lang.OutOfMemoryError:", 1).show(); } cursor.close(); } catch (Exception e) { e.printStackTrace(); } super.onActivityResult(requestCode, resultCode, data); } public static boolean checkBitmapFitsInMemory(long bmpwidth,long bmpheight, int bmpdensity ) { long reqsize=bmpwidth*bmpheight*bmpdensity; long allocNativeHeap = Debug.getNativeHeapAllocatedSize(); final long heapPad=(long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1); if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime().maxMemory()) { return false; } return true; }
Parag ghetiya
source share