Using Camera and storing captured result in SDCard in android

前端 未结 2 361
攒了一身酷
攒了一身酷 2020-12-21 06:22

I want to make application in which i want to use camera as after the image is captured, i want to store that image in SDCard and display that image in Screen too.

C

相关标签:
2条回答
  • 2020-12-21 06:57

    Use ACTION_IMAGE_CAPTURE intent to launch Camera Activity:

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
    startActivityForResult( intent, 0 );
    

    Have a detailed example here: http://labs.makemachine.net/2010/03/simple-android-photo-capture/ , which i think suitable to your problem as to capture image , store it in sd-card and then display image in imageview too.

    Enjoy!!!

    0 讨论(0)
  • 2020-12-21 07:05

    You can invoke system camera use below method:

    private void startCameraActivity() {
        File file = new File("/sdcard/test/test.jpg");
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, REQUEST_CODE_CAMERA);
    }
    

    when the invoke finished, you will get the picture use onActivityResult:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("MakeMachine", "requestCode:"+requestCode + ",resultCode: " + resultCode);
        switch(requestCode){
            case ModifyUserActivity.REQUEST_CODE_CAMERA:
                switch (resultCode) {
                    case Activity.RESULT_CANCELED:
                        picFileName = null;
                        Log.i("MakeMachine", "User cancelled");
                        break;
                    case Activity.RESULT_OK:                
                        File file = new File("/sdcard/test/test.jpg");
                        if(file.exists()){
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize = 4;
                            Bitmap bitmap = BitmapFactory.decodeFile(picFileName, options);
                            imgTakePhoto.setImageBitmap(bitmap);
                            imgTakePhoto.setVisibility(View.VISIBLE);
                        }
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题