SHARE
TWEET

Untitled

a guest Jun 25th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainActivity extends AppCompatActivity implements IImagePickerLister{
  2.  
  3.     Bitmap bitmap;
  4.     private ImageView imageView;
  5.     Button selectImg,uploadImg;
  6.     EditText imgTitle;
  7.     private  static final int IMAGE = 100;
  8.  
  9.     private ProgressBar progressBar;
  10.  
  11.  
  12.     private static final int CAMERA_ACTION_PICK_REQUEST_CODE = 610;
  13.     private static final int PICK_IMAGE_GALLERY_REQUEST_CODE = 600;
  14.     public static final int CAMERA_STORAGE_REQUEST_CODE = 611;
  15.     public static final int ONLY_CAMERA_REQUEST_CODE = 612;
  16.     public static final int ONLY_STORAGE_REQUEST_CODE = 613;
  17.  
  18.     private String currentPhotoPath = "";
  19.     private UiHelper uiHelper = new UiHelper();
  20.  
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         imageView = (ImageView) findViewById(R.id.imageView);
  28.         selectImg = (Button) findViewById(R.id.button);
  29.         uploadImg = (Button) findViewById(R.id.button2);
  30.         imgTitle = (EditText) findViewById(R.id.editText);
  31.  
  32.         progressBar = (ProgressBar) findViewById(R.id.progressBar_cyclic);
  33.  
  34.         findViewById(R.id.button).setOnClickListener(v -> {
  35.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  36.                 if (uiHelper.checkSelfPermissions(this))
  37.                     uiHelper.showImagePickerDialog(this, this);
  38.         });
  39.  
  40.         uploadImg.setOnClickListener(new View.OnClickListener() {
  41.             @Override
  42.             public void onClick(View v) {
  43.  
  44.  
  45. progressBar.setVisibility(View.VISIBLE);
  46.  
  47.                 validateImage();
  48.  
  49.  
  50.             }
  51.         });
  52.  
  53.     }
  54.  
  55.  
  56.  
  57.     private void validateImage() {
  58.  
  59.  
  60.  
  61.         //find values
  62.         final String regName = imgTitle.getText().toString();
  63.         String image = convertToString();
  64.  
  65.         //        checking if username is empty
  66.         if (TextUtils.isEmpty(regName)) {
  67.             Toast.makeText(MainActivity.this, "Seleziona i metodi di pagamento del tuo locale.", Toast.LENGTH_LONG).show();
  68.             return;
  69.         }
  70.         //checking if email is empty
  71.  
  72.         //checking if password is empty
  73.  
  74.         //After Validating we register User
  75.         uploadImage(regName, image);
  76.  
  77.  
  78.  
  79.     }
  80.  
  81.  
  82.     private String convertToString()
  83.     {
  84.  
  85.  
  86.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  87.         bitmap.compress(Bitmap.CompressFormat.WEBP,75,byteArrayOutputStream);
  88.  
  89.  
  90.         byte[] imgByte = byteArrayOutputStream.toByteArray();
  91.         return Base64.encodeToString(imgByte,Base64.DEFAULT);
  92.  
  93.  
  94.     }
  95.  
  96.  
  97.     private void uploadImage(String imageNome, String image){
  98.  
  99.         ApiInterface api = ApiClient.createService(ApiInterface.class);
  100.         Call<Img_Pojo> login = api.uploadImage(imageNome,image);
  101.  
  102.         login.enqueue(new Callback<Img_Pojo>() {
  103.             @Override
  104.             public void onResponse(Call<Img_Pojo> call, Response<Img_Pojo> response) {
  105.  
  106.                 if(Objects.requireNonNull(response.body()).getIsSuccess() == 1) {
  107.                     startActivity(new Intent(MainActivity.this,Main2Activity.class));
  108.  
  109.                     Toast.makeText(MainActivity.this, "Informazioni inserite!", Toast.LENGTH_LONG).show();
  110.  
  111.                     progressBar.setVisibility(View.GONE);
  112.  
  113.                 }
  114.  
  115.                  else{
  116.                 Toast.makeText(MainActivity.this,response.body().getMessage(),Toast.LENGTH_LONG).show();
  117.             }
  118.  
  119.  
  120.             }
  121.  
  122.             @Override
  123.             public void onFailure(Call<Img_Pojo> call, Throwable t) {
  124.                 Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
  125.  
  126.             }
  127.         });
  128.  
  129.     }
  130.  
  131.  
  132.  
  133.     @Override
  134.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  135.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  136.         if (requestCode == CAMERA_STORAGE_REQUEST_CODE) {
  137.             if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)
  138.                 uiHelper.showImagePickerDialog(this, this);
  139.             else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_DENIED) {
  140.                 uiHelper.toast(this, "ImageCropper needs Storage access in order to store your profile picture.");
  141.                 finish();
  142.             } else if (grantResults[0] == PackageManager.PERMISSION_DENIED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
  143.                 uiHelper.toast(this, "ImageCropper needs Camera access in order to take profile picture.");
  144.                 finish();
  145.             } else if (grantResults[0] == PackageManager.PERMISSION_DENIED && grantResults[1] == PackageManager.PERMISSION_DENIED) {
  146.                 uiHelper.toast(this, "ImageCropper needs Camera and Storage access in order to take profile picture.");
  147.                 finish();
  148.             }
  149.         } else if (requestCode == ONLY_CAMERA_REQUEST_CODE) {
  150.             if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
  151.                 uiHelper.showImagePickerDialog(this, this);
  152.             else {
  153.                 uiHelper.toast(this, "ImageCropper needs Camera access in order to take profile picture.");
  154.                 finish();
  155.             }
  156.         } else if (requestCode == ONLY_STORAGE_REQUEST_CODE) {
  157.             if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
  158.                 uiHelper.showImagePickerDialog(this, this);
  159.             else {
  160.                 uiHelper.toast(this, "ImageCropper needs Storage access in order to store your profile picture.");
  161.                 finish();
  162.             }
  163.         }
  164.     }
  165.  
  166.     @Override
  167.     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  168.         super.onActivityResult(requestCode, resultCode, data);
  169.         if (requestCode == CAMERA_ACTION_PICK_REQUEST_CODE && resultCode == RESULT_OK) {
  170.             Uri uri = Uri.parse(currentPhotoPath);
  171.             openCropActivity(uri, uri);
  172.         } else if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) {
  173.             if (data != null) {
  174.                 Uri uri = UCrop.getOutput(data);
  175.                 showImage(uri);
  176.             }
  177.         } else if (requestCode == PICK_IMAGE_GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
  178.             try {
  179.                 Uri sourceUri = data.getData();
  180.                 File file = getImageFile();
  181.                 Uri destinationUri = Uri.fromFile(file);
  182.                 openCropActivity(sourceUri, destinationUri);
  183.                 bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),sourceUri);
  184.  
  185.                 imageView.setImageBitmap(bitmap);
  186.             } catch (Exception e) {
  187.                 uiHelper.toast(this, "Please select another image");
  188.             }
  189.         }
  190.     }
  191.  
  192.     private void openImagesDocument() {
  193.         Intent pictureIntent = new Intent(Intent.ACTION_GET_CONTENT);
  194.         pictureIntent.setType("image/*");
  195.         pictureIntent.addCategory(Intent.CATEGORY_OPENABLE);
  196.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  197.             String[] mimeTypes = new String[]{"image/jpeg", "image/png"};
  198.             pictureIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
  199.         }
  200.         startActivityForResult(Intent.createChooser(pictureIntent, "Select Picture"), PICK_IMAGE_GALLERY_REQUEST_CODE);
  201.     }
  202.  
  203.     private void showImage(Uri imageUri) {
  204.         try {
  205.             File file;
  206.             if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
  207.                 file = FileUtils.getFile(this, imageUri);
  208.             } else {
  209.                 file = new File(currentPhotoPath);
  210.             }
  211.             InputStream inputStream = new FileInputStream(file);
  212.             Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
  213.             imageView.setImageBitmap(bitmap);
  214.         } catch (Exception e) {
  215.             uiHelper.toast(this, "Please select different profile picture.");
  216.         }
  217.     }
  218.  
  219.  
  220.  
  221.     private void openCamera() {
  222.         Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  223.         File file;
  224.         try {
  225.             file = getImageFile(); // 1
  226.         } catch (Exception e) {
  227.             e.printStackTrace();
  228.             uiHelper.toast(this, "Please take another image");
  229.             return;
  230.         }
  231.         Uri uri;
  232.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) // 2
  233.             uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID.concat(".provider"), file);
  234.         else
  235.             uri = Uri.fromFile(file); // 3
  236.         pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 4
  237.         startActivityForResult(pictureIntent, CAMERA_ACTION_PICK_REQUEST_CODE);
  238.     }
  239.  
  240.     private File getImageFile() throws IOException {
  241.         String imageFileName = "JPEG_" + System.currentTimeMillis() + "_";
  242.         File storageDir = new File(
  243.                 Environment.getExternalStoragePublicDirectory(
  244.                         Environment.DIRECTORY_DCIM
  245.                 ), "Camera"
  246.         );
  247.         System.out.println(storageDir.getAbsolutePath());
  248.         if (storageDir.exists())
  249.             System.out.println("File exists");
  250.         else
  251.             System.out.println("File not exists");
  252.         File file = File.createTempFile(
  253.                 imageFileName, ".jpg", storageDir
  254.         );
  255.         currentPhotoPath = "file:" + file.getAbsolutePath();
  256.         return file;
  257.     }
  258.  
  259.     private void openCropActivity(Uri sourceUri, Uri destinationUri) {
  260.         UCrop.Options options = new UCrop.Options();
  261.         options.setCropFrameColor(ContextCompat.getColor(this, R.color.colorAccent));
  262.         UCrop.of(sourceUri, destinationUri)
  263.                 .withMaxResultSize(1080, 540)
  264.                 .withAspectRatio(16, 9)
  265.                 .start(this);
  266.  
  267.  
  268.     }
  269.     @Override
  270.     public void onOptionSelected(ImagePickerEnum imagePickerEnum) {
  271.         if (imagePickerEnum == ImagePickerEnum.FROM_CAMERA)
  272.             openCamera();
  273.         else if (imagePickerEnum == ImagePickerEnum.FROM_GALLERY)
  274.             openImagesDocument();
  275.     }
  276. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top