Android 正在尝试在我的应用程序中显示刚使用内置照相机应用程序拍摄的照片

Android 正在尝试在我的应用程序中显示刚使用内置照相机应用程序拍摄的照片,android,Android,我正在尝试制作一个应用程序,允许用户拍摄照片,然后在应用程序中显示并保存 我正在使用下面的代码从谷歌上拍摄这张照片 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, 1); 我如何判断用户何时按下“后退”按钮并返回到我的应用程序,以便显示图像???使用此代码拍照 File pictureFileDir = getDir

我正在尝试制作一个应用程序,允许用户拍摄照片,然后在应用程序中显示并保存

我正在使用下面的代码从谷歌上拍摄这张照片

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);

我如何判断用户何时按下“后退”按钮并返回到我的应用程序,以便显示图像???

使用此代码拍照

File pictureFileDir = getDir();
                                if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

                                    Log.d("Photo Take", "Can't create directory to save image.");
                                    Toast.makeText(PhotoTake.this , "Can't create directory to save image.",
                                            Toast.LENGTH_LONG).show();
                                    return;
                                }

                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
                                String date = dateFormat.format(new Date());
                                String photoFile = "Picture_" + date + ".jpg";
                                String filepath = pictureFileDir.getPath() + File.separator;
                                File imageFile = new File(filepath , photoFile);

                                ContentValues image = new ContentValues();

                                image.put(Images.Media.TITLE, photoFile);
                                image.put(Images.Media.DISPLAY_NAME, photoFile);
                                image.put(Images.Media.DESCRIPTION, "Accident data Accachment " + date);
                                image.put(Images.Media.DATE_ADDED, date);
                                image.put(Images.Media.DATE_TAKEN, date);
                                image.put(Images.Media.DATE_MODIFIED, date);
                                image.put(Images.Media.MIME_TYPE, "image/jpeg");
                                image.put(Images.Media.ORIENTATION, 0);

                                 File parent = imageFile.getParentFile();
                                 String path = parent.toString().toLowerCase();
                                 String name = parent.getName().toLowerCase();
                                 image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
                                 image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
                                 image.put(Images.Media.SIZE, imageFile.length());

                                 image.put(Images.Media.DATA, imageFile.getAbsolutePath());

                                 mCapturedImageURI = PhotoTake.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);


                                Intent intent = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        mCapturedImageURI);

                                startActivityForResult(intent, req);
现在在onActivityResult中检索它

if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            if (mCapturedImageURI != null) {
                img1.setImageBitmap(getScaledBitmap(mCapturedImageURI));;

                System.out.println("Onactivity Result uri = " + mCapturedImageURI.toString());
            } else {
                Toast.makeText(PhotoTake.this, "Error getting Image",
                        Toast.LENGTH_SHORT).show();
            }

        }
您需要此方法来避免OutOfMemory异常

private Bitmap getScaledBitmap(Uri uri){
        Bitmap thumb = null ;
        try {
            ContentResolver cr = getContentResolver();
            InputStream in = cr.openInputStream(uri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize=8;
            thumb = BitmapFactory.decodeStream(in,null,options);
        } catch (FileNotFoundException e) {
            Toast.makeText(PhotoTake.this , "File not found" , Toast.LENGTH_SHORT).show();
        }
        return thumb ; 
    }

希望能有所帮助。

您在同一活动中启动了TractivityForresult

you need to call 
onActivityResult(int requestcode, int resultCode, Intent data){
if(resultCode==RESULT_OK){

if(requestCode==1){

    // get your data from the intent
   }
  }

}

可能的重复如果你正在遵循谷歌的教程,那么它会告诉你该做什么,当用户返回到你的应用程序时,你需要覆盖onActivityResult,而你就是从那里获得图像的