Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public class CameraFragment extends Fragment{
  2.  
  3. String mCurrentPhotoPath = "";
  4.  
  5. @Nullable
  6. @Override
  7. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  8. final int MyVersion = Build.VERSION.SDK_INT;
  9. if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
  10. if (!checkIfAlreadyhavePermission()) {
  11.  
  12. requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  13. } else {
  14. try {// I have added the try catch to call dispatchTakePictureIntent
  15. dispatchTakePictureIntent();
  16. } catch(IOException asd){
  17. Toast.makeText(getActivity(), "ERROR IN ON CREATE VIEW", Toast.LENGTH_LONG).show();
  18. }
  19. }
  20.  
  21. }
  22. return inflater.inflate(R.layout.fragment_camera, container, false);
  23. }
  24.  
  25. private boolean checkIfAlreadyhavePermission() {
  26. int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
  27. return result == PackageManager.PERMISSION_GRANTED;
  28. }
  29. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  30. switch (requestCode) {
  31. case 1: {
  32. if (grantResults.length > 0
  33. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  34. try { // I have added the try catch to call dispatchTakePictureIntent
  35. dispatchTakePictureIntent();
  36. } catch(IOException asd){
  37. Toast.makeText(getActivity(), "ERROR IN CHECK IF ALREADY...", Toast.LENGTH_LONG).show();
  38. }
  39.  
  40. } else {
  41. Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
  42. }
  43. break;
  44. }
  45. }
  46. }
  47.  
  48. ImageView SkimmedImageImg;
  49. @Override
  50. public void onViewCreated(View view, Bundle savedInstanceState){
  51. super.onViewCreated(view, savedInstanceState);
  52. SkimmedImageImg = (ImageView)view.findViewById(R.id.SkimmedImg);
  53. }
  54.  
  55. static final int REQUEST_IMAGE_CAPTURE = 1;
  56.  
  57. private void dispatchTakePictureIntent() throws IOException{
  58. Fragment CameraFragment = this;
  59. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  60. // Ensure that there's a camera activity to handle the intent
  61. if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
  62. // Create the File where the photo should go
  63. File photoFile = null;
  64. try {
  65. photoFile = createImageFile();
  66. } catch (IOException ex) {
  67. // Error occurred while creating the File
  68. return;
  69. }
  70. // Continue only if the File was successfully created
  71. if (photoFile != null) {
  72. Uri photoURI = FileProvider.getUriForFile(getActivity(),
  73. BuildConfig.APPLICATION_ID + ".provider",
  74. createImageFile());
  75. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
  76. CameraFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
  77. }
  78. }
  79. Toast.makeText(getContext(), "ERROR IN Dispatch!!", Toast.LENGTH_LONG).show();
  80. }
  81.  
  82. @Override
  83. public void onActivityResult(int requestCode, int resultCode, Intent data){
  84. super.onActivityResult(requestCode, resultCode, data);
  85. MainActivity fragment = new MainActivity();
  86. fragment.canUseButtons=true;
  87. Uri imageUri = Uri.parse(mCurrentPhotoPath);
  88. Glide.with(getActivity())
  89. .load(imageUri)
  90. .into(SkimmedImageImg);
  91. }
  92.  
  93. private File createImageFile() throws IOException {
  94. // Create an image file name
  95. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  96. String imageFileName = "JPEG_" + timeStamp + "_";
  97. File storageDir = new File(Environment.getExternalStoragePublicDirectory(
  98. Environment.DIRECTORY_DCIM), "Camera");
  99. File image = File.createTempFile(
  100. imageFileName, /* prefix */
  101. ".jpg", /* suffix */
  102. storageDir /* directory */
  103. );
  104.  
  105. // Save a file: path for use with ACTION_VIEW intents
  106. mCurrentPhotoPath = "file:" + image.getAbsolutePath();
  107. return image;
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement