如何仅在片段

时间:2017-04-11 20:12:25

标签: android android-fragments camera

以下代码打开相机但覆盖整个屏幕,并在捕获图像后返回到片段没有点击图像的迹象。 (图像安全地保存在图库中。我想仅在FRAGMENT部分打开相机。我复制了here中的代码。我刚刚删除了' viewHolder'(4rth try of try())as我不知道它在做什么。并且在我的代码中声明了URI变量 我也尝试了其他答案,例如this,但是当我在API 19上开发时,它们很复杂。



//my declared variables 
 private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
    Button button;
    ImageView imageView;
    Uri imageUri;


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        getActivity().startActivityForResult(intent, 100);


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scan, container, false);
    }


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 100:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = imageUri;
                    getActivity().getContentResolver().notifyChange(selectedImage, null);
                    ContentResolver cr = getActivity().getContentResolver();
                    Bitmap bitmap;
                    try {
                        bitmap = android.provider.MediaStore.Images.Media
                                .getBitmap(cr, selectedImage);

                        imageView.setImageBitmap(bitmap);
                        Toast.makeText(getActivity(), selectedImage.toString(),
                                Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
                                .show();
                        Log.e("Camera", e.toString());
                    }
                }
        }
    }




1 个答案:

答案 0 :(得分:1)

  

以下代码打开相机但覆盖整个屏幕

不确定。您正在推出第三方相机应用程序,通常会占用前景。

  

我想在FRAGMENT部分打开相机。

使用android.hardware.Camera和/或android.hardware.camera2.*类,从头开始编写您自己的相机代码。您无法将第三方相机应用程序启动到片段中。