1

I'm actually new in android but I managed to take a picture with my app and this is how I take a picture and save it... The problem is that I need to resize it before saving it on the phone... But I can't figure out how to to that.. I've googled my problem but the only thing I found was with bitmap pictures and that's not my case I guess..

Here is the code I'm using to take the picture:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"GUASTO" + System.currentTimeMillis() + ".jpg"); /

intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

startActivityForResult(intent, SCATTA_FOTO);

Thank you!!

ernestocattaneo
  • 1,197
  • 5
  • 18
  • 30

1 Answers1

0
public static int PIC_CROP=81;

   Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
                //indicate image type and Uri
            cropIntent.setDataAndType(ImageUri, "image/*");
                //set crop properties
                //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
                //indicate output X and Y
            cropIntent.putExtra("outputX", 640);
            cropIntent.putExtra("outputY", 640);
                //retrieve data on return
            cropIntent.putExtra("return-data", true);
                //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);

and in onActivityResult()

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
         if(requestCode==PIC_CROP)
        {
             if(resultCode==RESULT_OK)
             {
             Bundle extras = data.getExtras();
                if (extras != null) {               
                    Bitmap bmp = extras.getParcelable("data");
                    saveCropPhoto(bmp);
                }
              Toast.makeText(getApplicationContext(), "picture cropped",Toast.LENGTH_SHORT).show();
             }
        }

        }

saveCropPhoto() method

public void saveCropPhoto(Bitmap bmp)
        {
        Toast.makeText(getApplicationContext(), "in save",Toast.LENGTH_SHORT).show();
        String dir = Environment.getExternalStorageDirectory().toString() + "/folderName/"; 
        File newdir = new File(dir); 
        newdir.mkdirs();
        FileOutputStream out = null;
        Calendar c = Calendar.getInstance();
        String date = fromInt(c.get(Calendar.MONTH))
                    + fromInt(c.get(Calendar.DAY_OF_MONTH))
                    + fromInt(c.get(Calendar.YEAR))
                    + fromInt(c.get(Calendar.HOUR_OF_DAY))
                    + fromInt(c.get(Calendar.MINUTE))
                    + fromInt(c.get(Calendar.SECOND));
        File imageFileName = new File(newdir, "crop_"+date.toString() + ".jpg");
        try
        {
         out = new FileOutputStream(imageFileName);
         bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
         out.flush();
         out.close();
         out = null;
         if(tempFile.exists())
         tempFile.delete();
        } catch (Exception e)
        {
        e.printStackTrace();
        }
        }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • There is no crop intent: https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html – Bevor Apr 07 '18 at 12:09