Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CameraFragment extends Fragment{
- String mCurrentPhotoPath = "";
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- final int MyVersion = Build.VERSION.SDK_INT;
- if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
- if (!checkIfAlreadyhavePermission()) {
- requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
- } else {
- try {// I have added the try catch to call dispatchTakePictureIntent
- dispatchTakePictureIntent();
- } catch(IOException asd){
- Toast.makeText(getActivity(), "ERROR IN ON CREATE VIEW", Toast.LENGTH_LONG).show();
- }
- }
- }
- return inflater.inflate(R.layout.fragment_camera, container, false);
- }
- private boolean checkIfAlreadyhavePermission() {
- int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
- return result == PackageManager.PERMISSION_GRANTED;
- }
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case 1: {
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- try { // I have added the try catch to call dispatchTakePictureIntent
- dispatchTakePictureIntent();
- } catch(IOException asd){
- Toast.makeText(getActivity(), "ERROR IN CHECK IF ALREADY...", Toast.LENGTH_LONG).show();
- }
- } else {
- Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
- }
- break;
- }
- }
- }
- ImageView SkimmedImageImg;
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState){
- super.onViewCreated(view, savedInstanceState);
- SkimmedImageImg = (ImageView)view.findViewById(R.id.SkimmedImg);
- }
- static final int REQUEST_IMAGE_CAPTURE = 1;
- private void dispatchTakePictureIntent() throws IOException{
- Fragment CameraFragment = this;
- Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- // Ensure that there's a camera activity to handle the intent
- if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
- // Create the File where the photo should go
- File photoFile = null;
- try {
- photoFile = createImageFile();
- } catch (IOException ex) {
- // Error occurred while creating the File
- return;
- }
- // Continue only if the File was successfully created
- if (photoFile != null) {
- Uri photoURI = FileProvider.getUriForFile(getActivity(),
- BuildConfig.APPLICATION_ID + ".provider",
- createImageFile());
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
- CameraFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
- }
- }
- Toast.makeText(getContext(), "ERROR IN Dispatch!!", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data){
- super.onActivityResult(requestCode, resultCode, data);
- MainActivity fragment = new MainActivity();
- fragment.canUseButtons=true;
- Uri imageUri = Uri.parse(mCurrentPhotoPath);
- Glide.with(getActivity())
- .load(imageUri)
- .into(SkimmedImageImg);
- }
- private File createImageFile() throws IOException {
- // Create an image file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imageFileName = "JPEG_" + timeStamp + "_";
- File storageDir = new File(Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_DCIM), "Camera");
- File image = File.createTempFile(
- imageFileName, /* prefix */
- ".jpg", /* suffix */
- storageDir /* directory */
- );
- // Save a file: path for use with ACTION_VIEW intents
- mCurrentPhotoPath = "file:" + image.getAbsolutePath();
- return image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement