简体   繁体   English

如何从Android中的另一个活动调用takePicture()方法?

[英]How to call takePicture() method from another activity in android?

I have main activity class where i have written takePicturefromCamera() method like this: 我有一个主要的活动类,我在其中编写了如下的takePicturefromCamera()方法:

public void takePicturefromCamera() {
             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                try {

                    String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        mImageCaptureUri = Uri.fromFile(mFileTemp);
                    }
                    else {
                    //  The solution is taken from here: http://stackoverflow.com/questions/10042695/how-to-get-camera-result-as-a-uri-in-data-folder

                        mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
                    }   
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
                } catch (ActivityNotFoundException e) {

                    Log.d("TAG", "cannot take picture", e);
                }

        }

Now I have another class where i am doing some cropping part and my requirement is if while cropping user feels that he want to take some another picture instead of previous there i want to call above takePicturefromCamera() method of main activity class on some button click. 现在我有另一个类,我正在做一些裁剪部分,我的要求是,如果裁剪用户感觉他想再拍一张其他照片而不是以前的照片,我想在某些按钮单击时在主要活动类的takePicturefromCamera()方法上方调用。 Can anyone help me how can I do like this. 谁能帮助我,我该怎么做。 I have tried by doing like this: 我已经尝试过这样做:

retake_button.setOnClickListener(
                     new View.OnClickListener() {
                         public void onClick(View v) {
                            // setResult(RESULT_CANCELED);
                           main m = new main();
                           m.takePicturefromCamera();
                         }
                     });

But it gives me error NullPointerException at mImageCaptureUri = Uri.fromFile(mFileTemp); 但是它在mImageCaptureUri = Uri.fromFile(mFileTemp);时给了我NullPointerException错误mImageCaptureUri = Uri.fromFile(mFileTemp);

Please help me. 请帮我。

I would suggest you to keep takePicturefromCamera() in separate class, and then call this method passing the activitycontext and data required to it. 我建议您将takePicturefromCamera()保留在单独的类中,然后调用此方法,将activitycontext和所需的数据传递给它。

Or else you can make Activity2 extend Activity1 and use takePicturefromCamera() . 否则,您可以使Activity2 extend Activity1并使用takePicturefromCamera()

Make that method as static so you can use it anywhere in your application. 将该方法设置为静态,以便可以在应用程序中的任何位置使用它。

Also add one more parameter "File" to your function. 还要在函数中添加一个参数“ File”。 By this, you also have flexibility to use the same function for different file object in your application. 这样,您还可以灵活地对应用程序中的不同文件对象使用相同的功能。

Hope this will help you. 希望这会帮助你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM