在 Android 中使用 Intent 使用相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3574313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:48:59  来源:igfitidea点击:

Using intent to use Camera in Android

androidcamera

提问by Rakesh Gondaliya

I am using the following code to use camera by using intent. In the parameter of intent I am passing android.provider.MediaStore.ACTION_IMAGE_CAPTURE. It is able to open the camera. But the problem is that it stops unexpectedly. The problem is that it gives null pointer exception on OnActivityResults. I have used the below code:

我正在使用以下代码通过使用意图来使用相机。在我传递的意图参数中android.provider.MediaStore.ACTION_IMAGE_CAPTURE。它能够打开相机。但问题是它意外停止。问题是它在 上给出了空指针异常OnActivityResults。我使用了以下代码:

public class demo extends Activity {

Button ButtonClick;
int CAMERA_PIC_REQUEST = 2; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ButtonClick =(Button) findViewById(R.id.Camera);
    ButtonClick.setOnClickListener(new OnClickListener (){
        @Override
        public void onClick(View view)
        {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // request code

            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if( requestCode == CAMERA_PIC_REQUEST)
    {
    //  data.getExtras()
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
        image.setImageBitmap(thumbnail);
    }
    else 
    {
        Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

Can anyone help me to solve this problem?

谁能帮我解决这个问题?

回答by Aduait Pokhriyal

Try request code 1337.

尝试请求代码 1337。

startActivityForResult(cameraIntent , 1337);

回答by Ebin Sebastian

This how I use it

这是我如何使用它

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);

回答by Steve

do you have the following declarations in your manifest ?:

您的清单中是否有以下声明?:

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />

?? I used to do the same... here is my call to intent:

?? 我曾经做过同样的事情......这是我对意图的呼吁:

File file = new File( _path );
    Uri outputFileUri = Uri.fromFile( file );

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
    intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
    intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY,1);

the only slight difference between my and your code - I have file path in URI sending among call

我的代码和你的代码之间唯一的细微差别 - 我在调用之间发送的 URI 中有文件路径

回答by user3231100

I have used the following code and it worked!

我使用了以下代码并且它有效!

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        final Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        im.setImageDrawable(null);
        im.destroyDrawingCache();
        Bundle extras = data.getExtras();
        Bitmap imagebitmap = (Bitmap) extras.get("data");
        im.setImageBitmap(imagebitmap);
    }

}

回答by Ashutosh Srivastava

Using intent to use Camera in Android

在 Android 中使用 Intent 使用相机

##

##

   Uri imageUri;
1:- 

     TextView camera = (TextView)findViewById(R.id.camera);
         camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photo = new File(Environment.getExternalStorageDirectory(), new Date().getTime() + "myPic.jpg");
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photo));
            imageUri = Uri.fromFile(photo);
            startActivityForResult(cameraIntent, IMAGE_CAMERA_REQUEST);}


2:-



        @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                try {

                    if (requestCode == IMAGE_CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                        Uri selectedImage = imageUri;
                        getActivity().getContentResolver().notifyChange(selectedImage, null);
                        ContentResolver contentResolver = getActivity().getContentResolver();
                        Bitmap bitmap;
                        try {
                            bitmap = android.provider.MediaStore.Images.Media
                                    .getBitmap(contentResolver, selectedImage);

                            imageDialog(bitmap);
                        } catch (Exception e) {

                            Log.e("Camera", e.toString());
                        }

                    }
    }
    }}

I hope it's working for you.