I want to get image from the camera/gallery, compress it and send it to Firebase.
So I have done this (selectImage is the function to load from camera/gallery):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {
        if(requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) { //dalla fotocamera
            if(data != null) { //caso galleria
                mMediaUri = data.getData();
            }
            CompressBitmap task = new CompressBitmap(this);
            String newPath=null;
            try {
                newPath = task.execute(String.valueOf(mMediaUri)).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            Picasso.with(this).load(newPath).into(addPhoto, new Callback() {
                public void onSuccess() {
                    // stopProgressDialog(circleDialog);
                    showErrorMessage(RegisterActivity.this,getString(R.string.okAddPhoto));
                }
                @Override
                public void onError() {
                    showErrorMessage(RegisterActivity.this,getString(R.string.errAddPhoto));
                }
            });
        }
    } else {
    }
}
private void selectImage() {
    final CharSequence[] items = {getString(R.string.camera), getString(R.string.gallery), getString(R.string.indietro)};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.addPhoto);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals(getString(R.string.camera))) {
                mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO);
                dialog.dismiss();
            } else if (items[item].equals(getString(R.string.gallery))) {
                Intent pickPhoto = new Intent(Intent.ACTION_GET_CONTENT);
                pickPhoto.setType("image/*");
                startActivityForResult(pickPhoto,REQUEST_PICK_PHOTO);
                dialog.dismiss();
            } else if (items[item].equals(getString(R.string.indietro))) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}
private Uri getOutputMediaFileUri(int mediaTypeImage) {
    //check for external storage
    if(isExternalStorageAvaiable()) {
        File mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
       String fileName = "";
       String fileType = "";
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
       fileName = "IMG_"+timeStamp;
       fileType = ".jpg";
       File mediaFile;
       try {
           mediaFile = File.createTempFile(fileName,fileType,mediaStorageDir);
           Log.i("st","File: "+Uri.fromFile(mediaFile));
       } catch (IOException e) {
           e.printStackTrace();
           Log.i("St","Error creating file: " + mediaStorageDir.getAbsolutePath() +fileName +fileType);
           return null;
       }
       return Uri.fromFile(mediaFile);
   }
   //something went wrong
   return null;
}
private boolean isExternalStorageAvaiable() {
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    } else {
        return false;
    }
}
I only have problems when I load big images. So I think I should compress the bitmap image before loading it into the ImageView. So I found this code and I used it:
private class CompressBitmap extends AsyncTask<String, Void, String> {
    private Context context;
    private static final float maxHeight = 1280.0f;
    private static final float maxWidth = 1280.0f;
    public CompressBitmap(Context context) {
        this.context=context;
    }
    protected String doInBackground(String... strings) {
        if(strings.length == 0 || strings[0] == null) {
            return null;
        } return compressImage(strings[0]);
    }
    public String compressImage(String imagePath) {
        Bitmap scaledBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;
        float imgRatio = (float) actualWidth / (float) actualHeight;
        float maxRatio = maxWidth / maxHeight;
        Log.d("actual height, width:", String.valueOf(actualHeight) + "," + String.valueOf(actualWidth));
        Log.d("imgRatio,maxRatio", String.valueOf(imgRatio)+","+String.valueOf(maxRatio));
        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;
            }
        }
        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];
        try {
            bmp = BitmapFactory.decodeFile(imagePath, options);
            Log.d("bmp",String.valueOf(bmp));
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }
        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
            Log.d("scaledBitmap",String.valueOf(scaledBitmap));
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }
        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
        if (bmp != null) {
            bmp.recycle();
        }
        ExifInterface exif;
        try {
            exif = new ExifInterface(imagePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
            } else if (orientation == 3) {
                matrix.postRotate(180);
            } else if (orientation == 8) {
                matrix.postRotate(270);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream out = null;
        String filepath = imagePath;//getFilename();
        try {
            //new File(imageFilePath).delete();
            out = new FileOutputStream(filepath);
            //write the compressed bitmap at the destination specified by filename.
            scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return filepath;
    }
    public String getFilename() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                + "/ImageCompApp/Images");
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }
        String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
        String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
        return uriString;
    }
}
public static String getFilename() {
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/ImageCompApp/Images");
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        mediaStorageDir.mkdirs();
    }
    String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
    String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
    return uriString;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    final float totalPixels = width * height;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;
    while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
        inSampleSize++;
    }
    return inSampleSize;
}
But I get this error message:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
W/System.err: java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err:     at java.util.concurrent.FutureTask.report(FutureTask.java:94)
W/System.err:     at java.util.concurrent.FutureTask.get(FutureTask.java:164)
W/System.err:     at android.os.AsyncTask.get(AsyncTask.java:498)
W/System.err:     at com.sfproduction.happypark.RegisterActivity.onActivityResult(RegisterActivity.java:566)
W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6456)
W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
W/System.err:     at android.app.ActivityThread.-wrap16(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5461)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
W/System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
W/System.err:     at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                  Process: com.sfproduction.happypark, PID: 2494
                  Theme: themes:{}
                  java.lang.RuntimeException: An error occurred while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:309)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                      at java.lang.Thread.run(Thread.java:818)
                   Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
                      at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
                      at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
                      at android.os.AsyncTask$2.call(AsyncTask.java:295)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                      at java.lang.Thread.run(Thread.java:818) 
If I use InputStream I get bmp == null. 
I put all required permissions in the AndroidManifest.
How can I fix this error, or do this compression using other methods?
 
    