onActivityResult returned from a camera, Intent null

前端 未结 7 760
执念已碎
执念已碎 2020-12-06 04:41

I follow the instruction on Camera on Android dev site

I just start the Camera Intent, not build my own camera.

The sample code to handle result return afte

7条回答
  •  眼角桃花
    2020-12-06 05:40

    This code will help you

    The getData() method returns data only if Android version is newer than M, else it will return null result.

    if (resultCode == Activity.RESULT_OK) {
    
                    //Check Android version, as intent.getData() will return data only if v is above or equal to M otherwise the data will be null
                    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
                        Uri selectedImageURI = imageReturnedIntent.getData();
                        Toast.makeText(getActivity(), "URI Path:" + selectedImageURI, Toast.LENGTH_LONG).show();
                    } else {
                        Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                        String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), photo, "Title", null);
                        Uri selectedImageURI = Uri.parse(path);
                        Toast.makeText(getActivity(), "URI Path:" + selectedImageURI, Toast.LENGTH_LONG).show();
                    }
                }
    

提交回复
热议问题