How to open camera ONLY in a fragment

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

Following code opens camera but covers complete screen, and after capturing image it returns to the fragment No signs of clicked image. ( image is safely saved in gallery. I want to open camera ONLY IN FRAGMENT part. I have copied the code from here. I just removed 'viewHolder' (4rth line of try()) as I don't know what it was doing. and declared URI variable at top in my code.
I also have tried other answers like this but they are complicated as I am developing on 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:

Following code opens camera but covers complete screen

Sure. You are launching a third-party camera app, and those usually take over the foreground.

I want to open camera ONLY IN FRAGMENT part.

Write your own camera code from scratch, using android.hardware.Camera and/or the android.hardware.camera2.* set of classes. You cannot launch a third-party camera app into a fragment.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!