Guest User

Untitled

a guest
Dec 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. private void opnenCamera() {
  2. if (checker.lacksPermissions(PERMISSIONS_CAMERA_CAPTURE)) {
  3. startPermissionsActivity(PERMISSIONS_CAMERA_CAPTURE);
  4. } else {
  5.  
  6.  
  7. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  8. // Ensure that there's a camera activity to handle the intent
  9. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  10. // Create the File where the photo should go
  11. File photoFile = null;
  12. try {
  13. photoFile = createImageFile();
  14. } catch (IOException ex) {
  15. // Error occurred while creating the File
  16. ex.printStackTrace();
  17. }
  18. // Continue only if the File was successfully created
  19. if (photoFile != null) {
  20. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(MainActivity.this,
  21. BuildConfig.APPLICATION_ID + ".provider", destination));
  22. startActivityForResult(takePictureIntent, CAPTURE_IMAGE);
  23. }
  24. }
  25.  
  26. }
  27. }
  28.  
  29. private File createImageFile() throws IOException {
  30. // Create an image file name
  31. if (checker.lacksPermissions(PERMISSIONS_WRITE_EXTERNAL_STORAGE)) {
  32. startPermissionsActivity(PERMISSIONS_WRITE_EXTERNAL_STORAGE);
  33. } else {
  34. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  35. String imageFileName = timeStamp + "_";
  36. File storageDir =
  37. Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  38.  
  39. imageCamera = File.createTempFile(imageFileName, /* prefix */ //сам файл фото
  40. ".jpg", /* suffix */
  41. storageDir /* directory */);
  42.  
  43. // Save a file: path for use with ACTION_VIEW intents
  44. mCurrentPhotoPath = imageCamera.getAbsolutePath();//путь до фото
  45. Log.d("Getpath", "Cool" + mCurrentPhotoPath);
  46.  
  47. }
  48. return imageCamera;
  49. }
  50.  
  51. if ((requestCode == CAPTURE_IMAGE) && (data != null)) {
  52. isCamFoto = true;
  53.  
  54. // Get the dimensions of the View
  55. int targetW = imageViewUppload.getWidth();
  56. int targetH = imageViewUppload.getHeight();
  57.  
  58. // Get the dimensions of the bitmap
  59. BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  60. bmOptions.inJustDecodeBounds = true;
  61. BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  62. int photoW = bmOptions.outWidth;
  63. int photoH = bmOptions.outHeight;
  64.  
  65. // Determine how much to scale down the image
  66. int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
  67.  
  68. // Decode the image file into a Bitmap sized to fill the View
  69. bmOptions.inJustDecodeBounds = false;
  70. bmOptions.inSampleSize = scaleFactor;
  71.  
  72.  
  73. Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  74. imageViewUppload.setImageBitmap(bitmap);
  75.  
  76.  
  77. Toast.makeText(MainActivity.this, "Фото сохранено и выбрано!", Toast.LENGTH_LONG)
  78. .show();
  79.  
  80. } else {
  81. showError("Вы не выбрали файл");
  82. }
Add Comment
Please, Sign In to add comment