1

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

我正在使用以下代码拍照(来自 Google)

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

我如何知道用户何时按下后退按钮并返回我的应用程序以便我可以显示图像???

4

2 回答 2

0

使用此代码拍照...

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 ; 
    }

希望能帮助到你。

于 2013-10-14T18:32:58.730 回答
0

你在同一个活动中做了 startActivityForResult

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

if(requestCode==1){

    // get your data from the intent
   }
  }

}
于 2013-10-14T18:33:02.723 回答