相机套件库在真实设备上无法正常工作

时间:2019-07-14 03:30:31

标签: android android-camera android-camerax

我正在使用Android中的this(CameraKit)库来捕获图像。我将捕获的图像保存在文件中,然后尝试在另一个活动中显示该文件。我面临的问题是我的代码可以在模拟器上完美运行(我在各种android版本上尝试了大约4-5),但是当我在真实手机上对其进行测试时,图像不会被捕获。由于Log语句,我可以确认该按钮,但是我不知道图像是否被保存或是否正确地传递了意图。

代码如下:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_camera, container, false);



    cameraKitView = (CameraKitView) view.findViewById(R.id.camera);
    final MediaPlayer cameraclick = MediaPlayer.create(getContext(), R.raw.cameraclick);
    shutterButton = (ImageView) view.findViewById(R.id.shutter);
    gallery = (ImageView) view.findViewById(R.id.gallery1);
    videoCamera = (ImageView) view.findViewById(R.id.video);
    uploadText = (TextView) view.findViewById(R.id.uploadText);

    slidr = Slidr.attach(getActivity());

    shutterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("CameraActivity" , "Button pressed");
            cameraclick.start();
            cameraKitView.captureImage(new CameraKitView.ImageCallback() {
                @Override
                public void onImage(CameraKitView cameraKitView, final byte[] capturedImage) {
                    File savedPhoto = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
                    imageUri = Uri.fromFile(savedPhoto);
                    try {
                        FileOutputStream outputStream = new FileOutputStream(savedPhoto.getPath());
                        outputStream.write(capturedImage);
                        outputStream.close();
                        Intent intent = new Intent(getActivity() , ImageSelect.class);
                        intent.setData(imageUri);
                        startActivity(intent);
                    } catch (java.io.IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });

    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chooseImage();
        }
    });

    videoCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity() , VideoUpload.class);
            startActivity(intent);
        }
    });

    uploadText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity() , TextUpload.class);
            startActivity(intent);
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null )
    {
        imageUri = data.getData();
        //String test = filepath.toString();
        //Log.d("CameraActivity" , " Path is = " +test);
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
            //ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            //byte[] byteArray = stream.toByteArray();
            i = "1";
            Intent intent = new Intent(getActivity() , ImageSelect.class);
            intent.setData(imageUri);
            startActivity(intent);

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}

@Override
public void onStart() {
    super.onStart();
    cameraKitView.onStart();
}

private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
public void onResume() {
    super.onResume();
    cameraKitView.onResume();
}

@Override
public void onPause() {
    cameraKitView.onPause();
    super.onPause();
}

@Override
public void onStop() {
    cameraKitView.onStop();
    super.onStop();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

PS:正如我上面提到的,它在模拟器上可以正常工作。

0 个答案:

没有答案