0

Sir I have problem, when I want set LIMIT to UPLOAD IMAGE in KB (kilobytes), example 200KB.

private void uploadImage(){
    if (bitmap.getByteCount() > 7 * 1024 * 1024){
        H.T(AddFotoActivity.this,"Image Maks 200KB");
    }
}

I try that code but, I can only upload 110KB and when I try upload 116KB it can't upload. How to upload 200KB file sir? I don't know how to calculate 7*1024*1024

Thiago Souza
  • 1,093
  • 3
  • 13
  • 31
  • 1024 Bytes are 1 kiloByte. So **200kB * 1024B = 204800 Bytes** – adalpari Dec 21 '16 at 16:07
  • BUT, bitmaps are bigger in memory than the image file. So probabli an image file of 200k, would be much more when it gets open as a bitmap. – adalpari Dec 21 '16 at 16:09

1 Answers1

0

try this code:

/**
 * reduces the size of the image
 * @param image
 * @param maxSize
 * @return
 */
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 0) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

use it like:

Bitmap scaledImage = getResizedBitmap(photo, 300); //here 300 is maxsize

From Wikipedia

For highest quality images (Q=100), about 8.25 bits per color pixel is required

So, for Q=100 on an 300x300 image, that would result in (300 * 300) px * 8.25 bits/px = 741,500 bits = ~ 91 kB which is surely less than 200KB

you can try for other dimension too..

you can also try to make an image using the bitmap and compare the actual size of the image..

here is the code:

 //create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();

//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Your code is for widht and heigh resizing. So it doesn't mean 200k file resizing... Also, he is asking for another problem I think.. – adalpari Dec 21 '16 at 16:12
  • ok i may be wrong ...but take a look at this before give downvote http://stackoverflow.com/questions/16954109/reduce-the-size-of-a-bitmap-to-a-specified-size-in-android – rafsanahmad007 Dec 21 '16 at 16:19
  • Ok @rafsanahmad007 then link the answer and not only copy the code please. – adalpari Dec 21 '16 at 16:28
  • Although he is asking about how to calculate size and upload. Not about resizing. – adalpari Dec 21 '16 at 16:31
  • so what should I do sir for set limit upload image 200KB ? – Danzen Hangga Dec 22 '16 at 02:44
  • call this method Bitmap scaledImage = getResizedBitmap(photo, 300); //it will give you a 300X300 image which is less than 200KB..then you can upload image to your MYSQL database – rafsanahmad007 Dec 22 '16 at 04:23
  • see the edited answer..if it works don't forget to accept it – rafsanahmad007 Dec 22 '16 at 04:26
  • @rafsanahmad007 thanks sir that worked, amazing (om telolet om) – Danzen Hangga Dec 23 '16 at 08:10
  • @rafsanahmad007 sir i try to upload 17KB, but when i download the image from database, the the size of image is 25KB.. why the image became large ? how to bypass the convert image iif the image <200KB ?? i try with your sourcecode – Danzen Hangga Dec 28 '16 at 06:42
  • i think you server side do some modification on the image bitmap..bytes ..thats why it is large...my code only takes a bitmap as parameter and resize it before upload it to server – rafsanahmad007 Dec 28 '16 at 06:47
  • you can ask question how to check image size after modification from the server.. – rafsanahmad007 Dec 28 '16 at 06:48