-2

I have tried multiple solutions from the internet with no luck, How do I go about changing the image size when I upload on app? I want it to be in a way that when I upload a 2MB file,it gets sent to the server with size = 50kb. Please help me

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //get image thumbnail
    if (requestCode == REQUEST_CODE_PICKER && resultCode == RESULT_OK && data != null) {

        ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);

         absolutePath = null;
        // do your logic ....
        for (Image img : images) {
            Log.v(LOG_TAG, img.getName() + " " + img.getPath());
            absolutePath = img.getPath();
            absolutePath = String.valueOf(Compressor.getDefault(getContext()).compressToFile(imageFile));
            Bundle bundleExtras = data.getExtras();
            image = (Bitmap) bundleExtras.get("data");
        }
        consultantProfileImageView.setImageBitmap(getBitmapFromPath(absolutePath));
        new UploadConsultantProfileImageTask(getContext(), absolutePath).execute();
        postConsultant();
    }

}

public File getBitmapFromPath(String filePath) {
    File imageFile = new File(filePath);
    Bitmap imageBitmap = null;
    imageBitmap = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    InputStream in = new ByteArrayInputStream(bos.toByteArray());
    File compressedImageFile = Compressor.getDefault(getContext()).compressToFile(imageFile);
    if (compressedImageFile.exists()) {
        imageBitmap = BitmapFactory.decodeFile(compressedImageFile.getAbsolutePath());
    }

    return compressedImageFile;
}
Fuluza
  • 109
  • 2
  • 13
  • 3
    "I have tried multiple solutions from the internet with no luck" -- please explain what you tried (via a [mcve]) and what specifically "no luck" means with respect to what you tried. "when I upload a 2MB file" -- please explain **in detail** what sort of file this is (PNG, JPEG, RAW, GIF, WEBP, etc.) and where you have stored it. – CommonsWare Oct 17 '16 at 13:16
  • Do you want your app to compress the image or do you plan to do it manually? Please explain your question better.. – Ishan Manchanda Oct 17 '16 at 13:22
  • My app is supposed to allow user to add profile picture then crop/compress it to 50kb before saving it to server – Fuluza Oct 17 '16 at 13:25
  • the app has to compress it for me – Fuluza Oct 17 '16 at 13:26
  • Post your code and please be specific about your error. What is the problem you're facing? – Ishan Manchanda Oct 17 '16 at 13:37

2 Answers2

3

Try using below it will compress the image to 80 percent. You can change the percentage of compression according to your requirement.

public File getBitmapFromPath(String filePath) {
        File imageFile = new File(filePath);
        OutputStream  fout = new FileOutputStream(file);
        Bitmap bitmap= BitmapFactory.decodeFile(filePath);
        bitmap.compress(CompressFormat.JPEG, 80, fout); 
        fout.flush();
        fout.close();

        return imageFile;
}
  • I tried that it did not work. And for those who might face the same problems I did, I used this library found at https://android-arsenal.com/details/1/3758 , it works like a charm! – Fuluza Oct 18 '16 at 08:47
0

Try

int compressionRatio = 2; //1 == originalImage, 2 = 50% compression, 4=25% compress
File file = new File (imageUrl);
try {
    Bitmap bitmap = BitmapFactory.decodeFile (file.getPath ());
    bitmap.compress (Bitmap.CompressFormat.JPEG, compressionRatio, new FileOutputStream (file));
}
catch (Throwable t) {
    Log.e("ERROR", "Error compressing file." + t.toString ());
    t.printStackTrace ();
}
Mahmoud Zaher
  • 559
  • 6
  • 6