在savedInstanceState错误后加载位图图片

时间:2014-11-06 05:42:06

标签: android bitmap

Threr就是我所做的。在我的活动上。我正确地拍了2张照片但是如果我改变手机应用程序崩溃的方向

public class MyactivityTakePicture extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 //initialisation
   setContentView(R.layout.picture_main_layout);
    img1 = (ImageView)findViewById(R.id.photo1);
    img2 = (ImageView)findViewById(R.id.photo2);

    //creation des dossiers
    dir1 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/g0/"; 
    newdir1 = new File(dir1); 
    newdir1.mkdirs();

    dir2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/g1/"; 
   newdir2 = new File(dir2); 
   newdir2.mkdirs();

   capture1 = (Button) findViewById(R.id.btnCapture1);
capture1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        takepicture1();
    }
});

capture2 = (Button) findViewById(R.id.btnCapture2);
capture2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        takepicture2();

    }
});

if(savedInstanceState != null){
    filePath1 = savedInstanceState.getString("chemin1");
    filePath2 = savedInstanceState.getString("chemin2");
    bitmap1 = BitmapFactory.decodeFile(filePath1);
    img1.setImageBitmap(bitmap1);

    //filePath2 = savedInstanceState.getString("chemin1");
    Log.w("A", ""+filePath1);
    Log.i("F", ""+filePath2);
    bitmap2 = BitmapFactory.decodeFile(filePath2);
    img2.setImageBitmap(bitmap2);
}
}


 //prise de photo N°1
 ..................

//prise de photo N°2
protected void takepicture2(){
//nom des photos photo1.jpg, photo2.jpg ...
count++;
file2 = dir2+"photo.jpg";
File newfile2 = new File(file2);
filePath2 = dir2+"photo.jpg";
try {
    newfile2.createNewFile();
} catch (IOException e) {
    Log.e("Error", e.toString());
}       

Uri outputFileUri2 = Uri.fromFile(newfile2);

Intent cameraIntent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
cameraIntent2.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri2);

startActivityForResult(cameraIntent2, TAKE_PHOTO_CODE2);

}

//Enregistrement OK
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE1 && resultCode == RESULT_OK) {
        Log.d("CameraDemo1", "Pic saved");
        bitmap1 = BitmapFactory.decodeFile(filePath1);
        img1.setImageBitmap(bitmap1);

    }

    if (requestCode == TAKE_PHOTO_CODE2 && resultCode == RESULT_OK){
        Log.d("CameraDemo2", "Pic saved");
        bitmap2 = BitmapFactory.decodeFile(filePath2);
        img2.setImageBitmap(bitmap2);

    }
}

protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  if(newdir1==null){ 
      newdir1 = new File(file1);
  }// if

  if(newdir2==null){ 
      newdir2 = new File(file2);
  }
  outState.putString("chemin1", filePath1);
  outState.putString("chemin2", filePath2);

 }

}

首先,活动确实如此,拍摄两张照片。

如果我尝试更改手机的方向。她崩溃了。

请你看看?

1 个答案:

答案 0 :(得分:0)

问题是您的图片没有从内存中解除分配。

  java.lang.OutOfMemoryError: bitmap size exceeds VM budget

阅读这篇文章,对你有所帮助

<强> http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

<强> http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/

<强> http://mobi-solutions.blogspot.mx/2010/08/how-to-if-you-want-to-create-and.html

<强>更新 我已经看到了你的代码,我建议使用方法onDestroy()将位图实例设置为null:

@Override
protected void onDestroy(){
    super.onDestroy();
    bitmap1 = null;
    bitmap2 = null;
}