0
我有一個問題,我正在拍攝一張圖片並將其上傳到服務器。這是第一次,這一切都很好。 但第二次我正在圖片我在這條線得到一個內存溢出異常:位圖URI保持相同Android內存不足異常
_mPhoto = android.provider.MediaStore.Images.Media
.getBitmap(cr, _mUri);
如果我調試_mUri它是第一個1和第2次相同。我覺得這不應該..
我得到這個錯誤:http://pastebin.com/uVduy3d9
這些都是我用來拍攝照片3種方法:
如果手機有攝像頭首先檢查:
Camera cam = Camera.open();
if (cam != null) {
if (savedInstanceState != null
&& savedInstanceState.getBoolean("Layout")) {
setContentView(R.layout.registration_edit);
initializeAccountDetails((User) savedInstanceState
.getSerializable(EXTRA_MESSAGE));
inAccountDetails = true;
} else {
setContentView(R.layout.step_4);
((Button) findViewById(R.id.snap)).setOnClickListener(this);
((Button) findViewById(R.id.rotate)).setOnClickListener(this);
cam.stopPreview();
cam.release();
cam = null;
}
} else {
if (savedInstanceState != null
&& savedInstanceState.getBoolean("Layout")) {
setContentView(R.layout.registration_edit);
initializeAccountDetails((User) savedInstanceState
.getSerializable(EXTRA_MESSAGE));
inAccountDetails = true;
} else {
setContentView(R.layout.step_4b);
}
}
當按鈕點擊捕捉以下onClick事件被觸發:
@Override
public void onClick(View v) {
if (v.getId() == R.id.snap) {
File directory = new File(Environment.getExternalStorageDirectory()
+ "/BandenAnalyse/Images/");
if (directory.exists()) {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(),
"/BandenAnalyse/Images/IMG_" + _timeStamp + ".jpg");
_mUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, _mUri);
startActivityForResult(i, TAKE_PICTURE);
} else {
directory.mkdir();
this.onClick(v);
}
} else {
if (_mPhoto != null) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
_mPhoto = Bitmap.createBitmap(_mPhoto, 0, 0,
_mPhoto.getWidth(), _mPhoto.getHeight(), matrix, true);
((ImageView) findViewById(R.id.photo_holder))
.setImageBitmap(_mPhoto);
}
}
}
當拍攝照片,結果方法將被解僱:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(_mUri, null);
ContentResolver cr = getContentResolver();
try {
_mPhoto = android.provider.MediaStore.Images.Media
.getBitmap(cr, _mUri);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int scale = _mPhoto.getWidth()/width;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 8;
Debug.out(PATH_TO_PHOTO);
Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o);
_mPhoto = Bitmap.createScaledBitmap(
temp,
_mPhoto.getWidth()/scale, _mPhoto.getHeight()
/scale, false);
temp.recycle();
((ImageView) findViewById(R.id.photo_holder))
.setImageBitmap(_mPhoto);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
}
}
如果使用大於或等於11的API,那麼清單中的「useLargeheapSpace = true」。 –
看來你有內存泄漏。您可以在Eclipse中使用Memory Analyzer工具來查找它。我建議你在設置位圖之後添加_mPhoto.recycle(),並且還要將((ImageView)findViewById(R.id.photo_holder))定義爲類級ImageView變量,以防止每次創建新實例 –
也支持api的低於11.以上和等於11作品現在相當不錯,謝謝@AliImran – Baklap4