pradyut

Android Crop Image Save

Jun 19th, 2021
1,942
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.nvoids.pradyut.nvoidsapp;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.ActivityNotFoundException;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.pm.PackageManager;
  9. import android.graphics.Bitmap;
  10. import android.media.MediaScannerConnection;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.os.Environment;
  14. import android.util.DisplayMetrics;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.ImageView;
  19. import android.widget.Toast;
  20.  
  21. import androidx.appcompat.app.AppCompatActivity;
  22. import androidx.core.app.ActivityCompat;
  23. import androidx.core.content.FileProvider;
  24.  
  25. import java.io.BufferedOutputStream;
  26. import java.io.File;
  27. import java.io.FileOutputStream;
  28. import java.io.FileWriter;
  29. import java.io.InputStream;
  30. import java.io.OutputStream;
  31.  
  32. public class ProfileImage extends AppCompatActivity {
  33.     ImageView imageView;
  34.     Button buttonCamera, buttonGallery ;
  35.     File file;
  36.     private static Uri imageUri;
  37.     Long filetime;
  38.     private Context ct;
  39.     Object obj;
  40.     Intent CamIntent, GalIntent, CropIntent ;
  41.     public  static final int RequestPermissionCode  = 1 ;
  42.     DisplayMetrics displayMetrics ;
  43.     int width, height;
  44.     private static String[] PERMISSIONS_CAMERA = {
  45.             Manifest.permission.CAMERA
  46.     };
  47.     @Override
  48.     protected void onCreate(Bundle savedInstanceState) {
  49.         super.onCreate(savedInstanceState);
  50.         setContentView(R.layout.activity_profileimage);
  51.         imageView = (ImageView)findViewById(R.id.imageview);
  52.         buttonCamera = (Button)findViewById(R.id.button2);
  53.         buttonGallery = (Button)findViewById(R.id.button1);
  54.         EnableRuntimePermission();
  55.         verifyCameraPermissions(this);
  56.         ct = this;
  57.         obj = this;
  58.  
  59.         buttonCamera.setOnClickListener(new View.OnClickListener() {
  60.             @Override
  61.             public void onClick(View view) {
  62.                 ClickImageFromCamera() ;
  63.             }
  64.         });
  65.  
  66.         buttonGallery.setOnClickListener(new View.OnClickListener() {
  67.             @Override
  68.             public void onClick(View view) {
  69.                 GetImageFromGallery();
  70.             }
  71.         });
  72.  
  73.     }
  74.  
  75.     public static void verifyCameraPermissions(Activity activity) {
  76.         // Check if we have write permission
  77.         int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
  78.         if (permission != PackageManager.PERMISSION_GRANTED) {
  79.             // We don't have permission so prompt the user
  80.             ActivityCompat.requestPermissions(activity, PERMISSIONS_CAMERA, 1);
  81.         }
  82.     }
  83.  
  84.     public void ClickImageFromCamera() {
  85.         try {
  86.             filetime = System.currentTimeMillis();
  87.             CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  88.             file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids",
  89.                     "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
  90.  
  91.             imageUri = FileProvider.getUriForFile(this, "com.nvoids.provider",
  92.                     new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids",
  93.                             "file " +  filetime  + ".jpeg"));
  94.             //uri = Uri.fromFile(file);
  95.             CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
  96.             CamIntent.putExtra("return-data", true);
  97.             startActivityForResult(CamIntent, 0);
  98.         } catch (Exception e) {
  99.             Log.d("ListView1", "Capture from Camera exception: " + e.toString());
  100.         }
  101.     }
  102.  
  103.     public void GetImageFromGallery(){
  104.         GalIntent = new Intent(Intent.ACTION_PICK,
  105.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  106.         startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2);
  107.     }
  108.  
  109.     @Override
  110.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  111.         super.onActivityResult(requestCode, resultCode, data);
  112.         //Log.d("ListView1", "requestCode: " + requestCode);
  113.         if (requestCode == 0 && resultCode == RESULT_OK) {
  114.             try {
  115.                 //sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, imageUri));
  116.             } catch ( Exception e) {
  117.                 Log.d("ListView1", "Refresh gallery exception: " + e.toString());
  118.             }
  119.             String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids/file " + filetime +".jpeg";
  120.             try {
  121.                 MediaScannerConnection.scanFile(this,
  122.                         new String[]{ filename }, null,
  123.                         new MediaScannerConnection.OnScanCompletedListener() {
  124.                             public void onScanCompleted(String path, Uri uri) {
  125.                                 Log.d("ListView1", "ExternalStorageScanned " + path);
  126.                                 //Log.d("ListView1", "ExternalStorage -> uri=" + uri);
  127.                             }
  128.                         });
  129.             } catch (Exception e) {  Log.d("ListView1", "MediaScanner exception: " + e.toString());  }
  130.             imageUri=Uri.parse(filename);
  131.             Log.d("ListView1", "ImageUri: " + imageUri.toString());
  132.             ImageCropFunction();
  133.         } else if (requestCode == 2) {
  134.             if (data != null) {
  135.                 try {
  136.                     //Log.d("ListView1", "ImageUri: " + imageUri.toString());
  137.                 } catch ( Exception e) {  Log.d("ListView1", "ImageUri exception: " + e.toString());  }
  138.                 imageUri = data.getData();
  139.                 ImageCropFunction();
  140.             } else {
  141.                 Log.d("ListView1", "Data is null");
  142.             }
  143.         } else if (requestCode == 1) {
  144.             Log.d("ListView1", "Inside 1");
  145.             if (data != null) {
  146.                 Log.d("ListView1", "Data is not null");
  147.                 Bitmap bitmap = null;
  148.                 try {
  149.                     Bundle bundle = data.getExtras();
  150.                     bitmap = bundle.getParcelable("data");
  151.                     imageView.setImageBitmap(bitmap);
  152.                 } catch (Exception e) {
  153.                     Log.d("ListView1", "Image view set exception: "+ e.toString());
  154.                 }
  155.  
  156.                 try {
  157.                     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NVoids/toServer.jpeg");
  158.                     file.createNewFile();
  159.                     FileOutputStream fos = new FileOutputStream(file);
  160.                     final BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 8);
  161.                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
  162.                     bos.flush();
  163.                     bos.close();
  164.                     fos.close();
  165.                 } catch (Exception e) {
  166.                     Log.d("ListView1", "Save to file exception: "+ e.toString());
  167.                 }
  168.  
  169.             } else {
  170.                 Log.d("ListView1", "Data is null");
  171.             }
  172.         }
  173.     }
  174.  
  175.     public void ImageCropFunction() {
  176.         // Image Crop Code
  177.         try {
  178.             try {
  179.                 Log.d("ListView1", "ImageUri: " + imageUri.toString()); } catch ( Exception e) {  Log.d("ListView1", "ImageUri exception: " + e.toString());  }
  180.             CropIntent = new Intent("com.android.camera.action.CROP");
  181.             CropIntent.setDataAndType(imageUri, "image/*");
  182.             CropIntent.putExtra("crop", "true");
  183.             CropIntent.putExtra("outputX", 50000);
  184.             CropIntent.putExtra("outputY", 50000);
  185.             CropIntent.putExtra("aspectX", 1);
  186.             CropIntent.putExtra("aspectY", 1);
  187.             CropIntent.putExtra("scaleUpIfNeeded", true);
  188.             CropIntent.putExtra("return-data", true);
  189.             startActivityForResult(CropIntent, 1);
  190.         } catch (ActivityNotFoundException e) {
  191.             Log.d("ListView1", "ImageCropFunction Activity Not Found exception: " + e.toString());
  192.         } catch (Exception e) {
  193.             Log.d("ListView1", "ImageCropFunction exception: " + e.toString());
  194.         }
  195.     }
  196.     //Image Crop Code End Here
  197.  
  198.     public void EnableRuntimePermission(){
  199.         if (ActivityCompat.shouldShowRequestPermissionRationale(ProfileImage.this,
  200.                 Manifest.permission.CAMERA))
  201.         {
  202.             Toast.makeText(ProfileImage.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
  203.         } else {
  204.             ActivityCompat.requestPermissions(ProfileImage.this,new String[]{
  205.                     Manifest.permission.CAMERA}, RequestPermissionCode);
  206.         }
  207.     }
  208.  
  209.     @Override
  210.     public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
  211.         switch (RC) {
  212.             case RequestPermissionCode:
  213.                 if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
  214.                     //Toast.makeText(ProfileImage.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
  215.                 } else {
  216.                     Toast.makeText(ProfileImage.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
  217.                 }
  218.                 break;
  219.         }
  220.     }
  221.  
  222.  
  223. }
  224.  
RAW Paste Data