how to create a camera action common for all android devices

最后都变了- 提交于 2019-12-08 12:44:03

问题


in my app i want use the camera for capturing image and send to a server.

When the user opens the Camera part of my app, i want to show him the default camera of the device he is using. At present following is the two line code which i am using to capture image

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(intent, TAKE_RECEIPT);

When i run this code in a HTC device, after capturing the image it showed a preview of the image with a done and retake button. When i click done it moves to the previous activity.

when i use the code in a moto device, after capturing the image it showed a preview of the image with a insert, retake and cancel. When i click done it moves to the previous activity.

Like this it will be differing for all devices. So when i click the done or insert or any other button positive button of any devices i want to start the uploading process.

How to proceed this please help me....


回答1:


You can use below code to solve your problem...

protected void startCameraActivity() 

{

    // TODO Auto-generated method stub
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        startActivityForResult( intent, 0 );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        switch( resultCode )
        {
            case 0:
                break;

            case -1:
                onPhotoTaken();
                break;
        }
    }

    protected void onPhotoTaken()
    {

        ///write code here what you want to done after capture the image using device camera
    }

    @Override 
    protected void onRestoreInstanceState( Bundle savedInstanceState)
    {
        if( savedInstanceState.getBoolean( add_project1.PHOTO_TAKEN ) ) 
        {
            onPhotoTaken();
        }
    }

    @Override
    protected void onSaveInstanceState( Bundle outState ) 
    {
        outState.putBoolean( add_project1.PHOTO_TAKEN, _taken );
    }



回答2:


I'm not sure if the question is only about how to upload the image after the capture.. but if it is just it, I'll provide you a solution to handle it after you get back from the capture intent to your activity.

You can specify a path to save your image when you start the capture intent:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "image.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_RECEIPT);

You can then handle the upload in your onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == TAKE_RECEIPT) {
        if (resultCode == Activity.RESULT_OK) {             
                // user took a picture, upload it
                // outputFileUri contains the uri to your file
                // you should delete it when the upload completes

        } else if (resultCode == Activity.RESULT_CANCELED) {
                //user canceled - do nothing
        }
    }
}



回答3:


Actually the problem Arrives when we run this code on the Google nexus 1 device as it captures the images then when we click the done button exception arises..that is null pointer in Uri SelectedImage = data.getData();



来源:https://stackoverflow.com/questions/6125983/how-to-create-a-camera-action-common-for-all-android-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!