0

I am uploading images with loopj.

params.put("file_"+key+"", new File(String.valueOf(value.getOriginalPath())));

but i want to resize image before upload. Is there a way to resize image "on the fly" - without resizing->saving to SD->uploading? just resize im memory and upload - InputStream?

Thanks.

kalyan pvs
  • 14,495
  • 3
  • 39
  • 57
SERG
  • 3,383
  • 7
  • 34
  • 67

2 Answers2

7

You can use Bitmaps createScaledBitmap() as explained e.g. here

How about this one: How to Resize a Bitmap in Android?

EDIT - Sending bitmap to server

A bitmap in memory is quite large as it is not compressed like a PNG or JPEG, instead for every pixel you need 3 or even 4 bytes of memory. Thus you'll want to convert that bitmap into e.g. an PNG before uploading. This can be done with the Bitmap.compress(...) method, which requires an OutputStream to write the compressed image to. As you do not want to save the image to SD card before uploading, you can use an ByteArrayOutputStream instead to keep the compressed image in memory.

Then, you can attach an ByteArrayInputStream to the bytes written and pass that to loopj.

In code this should look something like so:

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, quality, outStream);
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

/* ...and then pass inStream loopj */
Community
  • 1
  • 1
Ridcully
  • 22,583
  • 7
  • 66
  • 81
  • ok i scaled image `Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); ` but how to put it into loopj params? it supports InputStream, File, ByteArrayInputStream – SERG Nov 16 '14 at 12:53
  • I added an example on how to do that to the answer. – Ridcully Nov 16 '14 at 16:47
  • got an OutOfMemoryError on `AsyncHttpClient client = new AsyncHttpClient(); client.post(HERE);` is there a simple and right way to upload pictures on server? – SERG Nov 17 '14 at 15:38
  • I don't know loopj that well. Have you checked their documentation? – Ridcully Nov 18 '14 at 10:47
  • i just need a simple and right way to upload resized image to server. is there a good example or algoritm? 1. resize image 2. save it to temp dir??? as i see it costs too much memory to store images in memory, as i need more than 3 images at once to upload 3. upload image – SERG Nov 18 '14 at 15:39
  • You can create a temp file and change the example from above so that outStream and inStream go to that temp file instead of the in-memory byte array. – Ridcully Nov 19 '14 at 08:49
  • Why this method create a local file in memory of the user? – luke cross Jul 25 '20 at 14:03
0
Target mTarget = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
        Log.d("!!!!!",">>>>>>>>>>>>");
        SaveImage.SaveImageFromBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        Log.d("!!!!!","--------");
    }

    @Override
    public void onPrepareLoad(Drawable drawable) {

    }
};
File file = new File(value.getOriginalPathString());
Picasso.with(AddSpa.this).load(file).resize(800,0).into(mTarget);
JJD
  • 44,755
  • 49
  • 183
  • 309
SERG
  • 3,383
  • 7
  • 34
  • 67