美文网首页
调用camera学习

调用camera学习

作者: Crane_FeiE | 来源:发表于2018-11-01 16:05 被阅读0次

    调用摄像头拍照or利用外部存储文件设置头像

    • 摄像头直接上代码,就是些api的调用
    • 从相册中读取图片需要利用系统提供的provider来获取到该图片的路径,android 8 后强制使用这种方法。
    public class CameraTestActivity extends AppCompatActivity implements View.OnClickListener {
        public static final int TAKE_PHOTO = 1;
        public static final int CHOOSE_PHOTO = 2;
    
        private ImageView avaterImage;
        private Button takePhotoButton;
        private Button choosePhotoButton;
        private Uri imageUri;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_camera_test);
            init();
        }
    
        private void init() {
            avaterImage = findViewById(R.id.image_avatar);
            takePhotoButton = findViewById(R.id.button_camera);
            choosePhotoButton = findViewById(R.id.button_album);
    
            takePhotoButton.setOnClickListener(this);
            choosePhotoButton.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_camera:
                    File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
                    try {
                        if (outputImage.exists()) {
                            outputImage.delete();
                        }
                        outputImage.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                    //second parameter can be any unique string?
                    imageUri = FileProvider.getUriForFile(
                            this,
                            "com.example.crane.mufirstline.cameratestactivity.fileprovider",
                            outputImage);
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, TAKE_PHOTO);
                    break;
                case R.id.button_album:
                    if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
                    } else {
                        openAlbum()
                    }
                default:
                    break;
    
            }
        }
    
        private void openAlbum() {
            Intent intent = new Intent("android.intent.action.GET_CONTENT");
            intent.setType("image/*");
            startActivityForResult(intent, CHOOSE_PHOTO);
        }
    
        private void handleImage(Intent data) {
            String imgPath = null;
            Uri uri = data.getData();
            if (DocumentsContract.isDocumentUri(this, uri)) {
                //if the type is document, than handle it using doc id
                if ("com.android.provides.media.documents".equals(uri.getAuthority())) {
                    String docId = DocumentsContract.getDocumentId(uri);
                    String id = docId.split(":")[1];
                    String selection = MediaStore.Images.Media._ID + "=" + id;
                    imgPath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
                }
            }
            if (imgPath != null && !imgPath.isEmpty()) {
                displayImg(imgPath);
            }
        }
    
    
        private String getImagePath(Uri uri, String selection) {
            String path = null;
            Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }
                cursor.close();
            }
            return path;
        }
    
        private void displayImg(String imgPath) {
            if (imgPath != null) {
                Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
                avaterImage.setImageBitmap(bitmap);
            } else {
                Log.w("lyh", "null path!!");
            }
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case TAKE_PHOTO:
                    if (resultCode == RESULT_OK) {
                        try {
                            InputStream inputStream = getContentResolver().openInputStream(imageUri);
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            avaterImage.setImageBitmap(bitmap);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case CHOOSE_PHOTO:
                    if (resultCode == RESULT_OK) {
                        if(null == data){
                            break;
                        }
                        handleImage(data);
                    }
                    break;
                default:
                    break;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:调用camera学习

          本文链接:https://www.haomeiwen.com/subject/jjdhnftx.html