報oom錯誤的代碼:canvas
public void onPictureTaken(byte[] data, Camera camera) { Bitmap foto = BitmapFactory.decodeByteArray(data, 0, data.length); wid = foto.getWidth(); hgt = foto.getHeight(); Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(newImage); canvas.drawBitmap(foto, 0f, 0f, null); if (newImage.getWidth() > newImage.getHeight()) { Matrix matrix = new Matrix(); matrix.postRotate(90); newImage.createBitmap(newImage, 0, 0, wid, hgt, matrix, true); }}
修改後的代碼:
post
Bitmap bm = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 5; AssetFileDescriptor fileDescriptor =null; try { fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,」r」); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ try { bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options); fileDescriptor.close(); } catch (IOException e) { e.printStackTrace(); } }