0

I am facing a problem I cannot solve myself.

I have to capture image in my android app, and then upload that image to FTP server. Of course, I have to resize it before sending it to FTP because 2MB is definitely unacceptable size :)

I succeded in taking picture, getting its path and upload it in its full size.

This is how I upload it to server.

File file = new File(pathOfTheImage);
String testName =System.currentTimeMillis()+file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);

Is it possible, at this point, to resize or compress image in order to reduce its size and after that, to upload it to server?

Any help would be appreciated.

P.S. Sorry for my poor English!

EDIT:

Solved thanks to Alamri. One more time, man, thank you!!!

Jovan
  • 3
  • 1
  • 3
  • Your English is fine. Check out http://stackoverflow.com/questions/10413659/how-to-resize-image-in-android . Hope that helps. –  Jul 15 '13 at 01:35
  • @Yatin Saraiya Thank you for your (quick) answer! My problem is that at this point I am only having path of (full size) image. What would be the best to do? Sorry for so many questions, but this is really something I am trying to solve for long time and, unfortunately, I am being unsuccesfull in it. – Jovan Jul 15 '13 at 01:45
  • You need to download the image to re size or compress it. You can delete it after you upload it to the server. – nedaRM Jul 15 '13 at 01:49

1 Answers1

0

In my application i used this before uploading the image:
1- resize,scale and decode the bitmap

private Bitmap decodeFile(File f) {
    try {

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        final int REQUIRED_SIZE=450;

        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        return bit1;
    } catch (FileNotFoundException e) {}
    return null;
}

2- now let's save the resized bitmap:

private void ImageResizer(Bitmap bitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Pic");    
    if(!myDir.exists()) myDir.mkdirs();
    String fname = "resized.im";
    File file = new File (myDir, fname);
    if (file.exists()){
        file.delete();
        SaveResized(file, bitmap);
    } else {
        SaveResized(file, bitmap);
    }
}

private void SaveResized(File file, Bitmap bitmap) {
    try {
           FileOutputStream out = new FileOutputStream(file);
           bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    } catch (Exception e) {
           e.printStackTrace();
    }
}

after saving the resized, scaled image. upload it using your code :

String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Pic/resized.im");
    String testName =System.currentTimeMillis()+file.getName();
    fis = new FileInputStream(file);
    // Upload file to the ftp server
    result = client.storeFile(testName, fis);
Alamri
  • 2,112
  • 2
  • 16
  • 21
  • Man, I do not know how to thank you!!!!!!!!!! You saved me, I do not know how to thank you! Thank you, one million times!!! – Jovan Jul 15 '13 at 02:07
  • No problem, but don't forget to change the saved image extention, i used `.im` , you can use JPG, PNG .... :). – Alamri Jul 15 '13 at 02:13
  • @Jovan you're welcome, and another recommend .. not use FTP is NOT secure.. use multiple uploading. – Alamri Jul 15 '13 at 02:38
  • Yes, sure, but this is just something I am trying to do for so many hours and I paid more attention to image resize than to the things that are of bigger importance :) – Jovan Jul 15 '13 at 02:44