下面的代码有两个问题。它只是使用相机android的意图拍摄照片“onclick”,并将图像显示在ImageView上。
- 在没有离开活动的情况下拍摄两三张图片后,当我旋转显示器时,它经常会因内存不足错误而崩溃。
- 当我第一次拍照时,它会刷新图像视图但是当我第二次或第三次拍照时......它不会刷新图片,直到我旋转屏幕
我想将图片保存在内部存储器而不是外部存储器上,但我不明白该怎么做,因为我尝试了几个教程并且它卡住了相机!
公共类 HandScryActivity 扩展 Activity {
private static int TAKE_PICTURE = 1; private MtgMatch myMatch; private File handFile; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.handscry); // Disable screen saver getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON); // Load match myMatch = MtgMatch.getSingletonMtgMatch(); handFile = new File(Environment.getExternalStorageDirectory(), "test.jpg"); if (myMatch.getHandUri() != null) { loadPicture(); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); loadPicture(); } // Handles onGame clicked buttons public void btnHandClick(View v) { Button clickedButton = (Button) v; // according to clicked button switch (clickedButton.getId()) { case R.id.btnBackToGame: this.finish(); break; case R.id.btnTakePicture: myMatch.setHandUri(Uri.fromFile(handFile)); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, myMatch.getHandUri()); startActivityForResult(intent, TAKE_PICTURE); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE) { // Display image if (resultCode == RESULT_OK) { loadPicture(); } else if (resultCode == RESULT_CANCELED) { // User cancelled the image capture } else { // Image capture failed, advise user } } } // Put the photo inside frame private void loadPicture() { ImageView img = (ImageView) findViewById(R.id.imgHand); img.setImageURI(myMatch.getHandUri()); } }