简体   繁体   English

如何从onActivityResult(int requestCode,int resultCode,Intent data)获取结果?

[英]How can I get a result from an onActivityResult(int requestCode, int resultCode, Intent data)?

I'm working on an Android project which allow the user to take a picture using the camera. 我正在开发一个允许用户使用相机拍照的Android项目。

My method looks like this : 我的方法看起来像这样:

private void takePicture(){
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(camera, 1337);
    }

And in my onActivityResult I take the picture and store it in my SdCard. 在我的onActivityResult中,我拍摄照片并将其存储在我的SdCard中。 I trying to modify my takePicture() function to return the path of the picture (String instead void). 我试图修改我的takePicture()函数来返回图片的路径(String而不是void)。

I already know that getPath() from java.io.File allow me to do that. 我已经知道java.io.File中的getPath()允许我这样做。

So my question is : how can I modify my function to return the path of the picture? 所以我的问题是:如何修改我的函数以返回图片的路径?

takePicture won't be able to return a path, as it'll return before your intent has completed. takePicture将无法返回路径,因为它将在您的意图完成之前返回。 You'll need to do something with the path inside onActivityResult 你需要对onActivityResult的路径做一些事情

Here is the code to get the path of captured and stored image in pictureCallback 以下是获取pictureCallback中捕获和存储图像路径的代码

in this code mFilePath is the string which is holding the path of image... 在这段代码中, mFilePath是保存图像路径的字符串......

PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

                String mFilePath = pictureFile.getAbsolutePath(); 
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我进行Facebook登录时,onActivityResult(int requestCode,int resultCode,Intent data)中的数据意图等于null - data intent equal null in onActivityResult(int requestCode, int resultCode, Intent data) when i make facebook login onActivityResult中的requestCode和resultCode指的是什么? - What does requestCode and resultCode in onActivityResult refer to? 如何以INT_MAX模数得到结果? - How can I get the result modulo INT_MAX? requestCode = -1和resultCode = 0选择器来自图库或相机的图像的意图 - requestCode = -1 and resultCode = 0 chooserIntent for image from gallery or camera 找不到符号方法onActivityResult(int,int,Intent) - cannot find symbol method onActivityResult(int,int,Intent) 如何? 我可以从 Int 获得 Upper Char 值吗? - How? from an Int can I get the Upper Char value? 我如何从字符串获取整数数组 - how can i get int array from the string 相机 onActivityResult : resultCode 是 RESULT_CANCELED - Camera onActivityResult : resultCode is RESULT_CANCELED 在Java 8中,如何从Stream获取int数组 <int[]> 不使用forEach - in Java 8, how can I get int array from Stream<int[]> without using forEach 我如何从EditText获取值并将其转换为int类型 - How can i get value from EditText and convert it to int type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM