【问题标题】:stuck on image capture screen ,can't get back to onActivityResult (Android)卡在图像捕获屏幕上,无法返回 onActivityResult (Android)
【发布时间】:2012-10-17 09:23:11
【问题描述】:

我有一个通过启动 ACTION_IMAGE_CAPTURE Intent 来打开相机的活动:

Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    SimpleDateFormat dateformat = new SimpleDateFormat("ddMMyy");


    File photo1 = new File(Environment
            .getExternalStorageDirectory(), imageName);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1));
    startActivityForResult(intent, 5);

启动此意图后,它会打开图像捕获屏幕,有时在单击捕获按钮后它不会返回到我的应用程序 (onActivityResult),它会强制我再次拍摄图像,并且不会关闭它仅当我按下后退按钮时才显示屏幕。

我在调试的时候在OnActivityResult中放了一个断点,在这个方法中并没有停止。

【问题讨论】:

    标签: android android-camera android-camera-intent image-capture


    【解决方案1】:

    这是一个示例应用程序,它提供了从使用相机或图库中选择图像的选项,它还允许裁剪图像的选项。我希望这正是您想要的。

    请看这个https://github.com/lorensiuswlt/AndroidImageCrop

    它的开源下载它并探索。

    【讨论】:

      【解决方案2】:

      这是我的相机意图代码。它工作得很好,你可以尝试使用它。

      import android.app.Activity;
      import android.app.Dialog;
      import android.content.Context;
      import android.content.Intent;
      import android.graphics.Bitmap;
      import android.media.MediaPlayer;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.KeyEvent;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.ImageButton;
      import android.widget.Toast;
      
      public class PlayMenuActivity extends Activity implements OnClickListener {
      
          Intent intent;
          ImageButton cameraBtn, galleryBtn, maleBtn, femaleBtn;
          static Bitmap photo;
          Dialog dialog;
          static String gender;
          MediaPlayer mp; 
      
          protected static final int PHOTO_PICKED = 0;
          private static final int CAMERA_REQUEST = 1337;
          private static final int SELECT_PHOTO = 100;
          private static final String TEMP_PHOTO_FILE = "tempPhoto.jpg";
          protected boolean circleCrop = true;
          private final static String TAG = "GetImageFromGalleryActivity";
      
      
          // values for scaling image
          protected int outputX = 320;
          protected int outputY = 480;
          protected int aspectX = 2;
          protected boolean scale = true;
          protected int aspectY = 3;
          protected boolean return_data = false;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              Log.i("hello", "oncreate PlayMenu");
              setContentView(R.layout.play_menu_screen);//setting layout
              //Image Buttons on activity screen
              cameraBtn = (ImageButton) findViewById(R.id.cameraBtn_id);
              galleryBtn = (ImageButton) findViewById(R.id.galleryBtn_id);
              cameraBtn.setOnClickListener(this);
              galleryBtn.setOnClickListener(this);
              // sound played when button clicked
              mp = MediaPlayer.create(this, R.raw.click);
      
      
      
          }
      
          public void onClick(View v) {
              switch (v.getId()) {
              case R.id.cameraBtn_id:
                  // camera intent for starting camera
                  Intent cameraIntent = new Intent(
                          android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  cameraIntent.putExtra("crop", "true");
                  cameraIntent.putExtra("aspectX", aspectX);
                  cameraIntent.putExtra("aspectY", aspectY);
                  cameraIntent.putExtra("outputX", outputX);
                  cameraIntent.putExtra("outputY", outputY);
                  cameraIntent.putExtra("scale", scale);
                  cameraIntent.putExtra("category", "camera");
                  startActivityForResult(cameraIntent, CAMERA_REQUEST);
      
                  break;
      
              case R.id.galleryBtn_id:
                  // calling GalleryActivity for picking image from gallery
                  intent = new Intent(PlayMenuActivity.this, GalleryActivity.class);
      
                  startActivity(intent);
                  break;
      
              default:
                  break;
              }
      
          }
      
          /* creates bitmap of the captured photo */
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      
              // Log.i("hello", "REQUEST cALL");
              //if a camera request is made and resultcode matches then bitmap is created
              if (requestCode == CAMERA_REQUEST || resultCode == Activity.RESULT_OK) {
                  Log.i("hello", "REQUEST cALL");
                  try {
                      Log.i("hello", "Try Call");
                      Bitmap bMap = (Bitmap) data.getExtras().get("data");//creating bitmap 
      
      
                      photo = bMap;
                      Intent intent = new Intent(PlayMenuActivity.this,
                              ShowActivity.class);
                      intent.putExtra("category", "camera");//adding category selected ie camera
                      startActivity(intent);
      
                  } catch (Exception e) {
                      Log.i("hello", "Exception" + e.getMessage());
                  }
      
              } else {
                  // Log.i("hello", "Else call");
      
                  Toast.makeText(PlayMenuActivity.this, "Picture NOt taken",
                          Toast.LENGTH_LONG).show();
              }
      
          } // fn
      
      
      
      }// class
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        这已经很晚了,但这里的问题可能是缺少读/写权限。我自己也遇到了这个问题,这与我想将图像文件放入我自己的应用程序文件夹有关,而相机应用程序无法访问该文件夹。

        为了安全起见,在应用之间共享文件时,请考虑使用Environment.getExternalStoragePublicDirectory(),而不是Environment.getExternalStorageDirectory()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-20
          • 2013-07-23
          • 2017-03-24
          • 1970-01-01
          • 2015-09-03
          • 1970-01-01
          • 2010-12-04
          • 1970-01-01
          相关资源
          最近更新 更多