0

I am using this code to compress image ..and ftp to upload images.i get compressed image in my device but the image in backend is corrupted.i have camera and gallery selection option to upload images.

    public String compressImage(String filePath) {

    Bitmap scaledBitmap = null;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

    int actualHeight = options.outHeight;
    int actualWidth = options.outWidth;
    float maxHeight = 1200.0f;
    float maxWidth = 600.0f;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;

    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;
    }
    }
    ImageLoadingUtils utils = new ImageLoadingUtils(getApplicationContext());
    options.inSampleSize = utils.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(filePath, options);
    } catch (OutOfMemoryError exception) {
    exception.printStackTrace();

    }
    try {
    scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
    } 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));


    ExifInterface exif;
    try {
     exif = new ExifInterface(filePath);

    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
    Log.d("EXIF", "Exif: " + orientation);
    Matrix matrix = new Matrix();
    if (orientation == 6) {
    matrix.postRotate(90);
    Log.d("EXIF", "Exif: " + orientation);
    } else if (orientation == 3) {
    matrix.postRotate(180);
    Log.d("EXIF", "Exif: " + orientation);
    } else if (orientation == 8) {
    matrix.postRotate(270);
    Log.d("EXIF", "Exif: " + orientation);
    }
    scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
    } catch (IOException e) {
    e.printStackTrace();
    }
    FileOutputStream out = null;
    String filename = getFilename();
    try {
    out = new FileOutputStream(filename);
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    return filename;
    }


    public String getFilename() {
    File file = new File(Environment.getExternalStorageDirectory().getPath());
    if (!file.exists()) {
    file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
    return uriSting;
    }

  Async task============================================================  
    private class SellAsync extends AsyncTask<String, String, String>
    {
    final ProgressDialog progressDialog = new ProgressDialog(Sell_Main.this);
    long totalSize = 0;


    @Override
    protected String doInBackground(String... urls)
    {

    String msg = "";
    try
    {
    for (int i = 0; i < imageList.size(); i++)
    {
    String filePath = compressImage(imageList.get(i));

    File f = new File(filePath);

    new FileUploader().uploadFile(Sell_Main.this, f);
    }
    if (!FileUploader.flag) {
    throw new Exception("File uploading failed");
    }

    ArrayList<NameValuePair> arr = new ArrayList<NameValuePair>();
    arr.add(new BasicNameValuePair("brand", brand));
    arr.add(new BasicNameValuePair("model", model));
    arr.add(new BasicNameValuePair("variant", variant));
    arr.add(new BasicNameValuePair("fuel_type", fuelspinner));
    arr.add(new BasicNameValuePair("kilometres", km));
    arr.add(new BasicNameValuePair("transmission", trans
         arr.add(new BasicNameValuePair("make_year", makeyear));
    arr.add(new BasicNameValuePair("Expected_price", price));
    arr.add(new BasicNameValuePair("ownership", owner));
    arr.add(new BasicNameValuePair("location", location));
    arr.add(new BasicNameValuePair("cid", sPref.getString("id", "")));

    String imagesList = "";
    for (int i = 0; i < imageList.size(); i++) {
    File f = new File(imageList.get(i));
    imagesList += f.getName() + ",";
    }
    arr.add(new BasicNameValuePair("images", imagesList.substring(0, imagesList.length() - 1)));

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(urls[0]);

    post.setEntity(new UrlEncodedFormEntity(arr));

    HttpResponse response = client.execute(post);
    msg = EntityUtils.toString(response.getEntity());

    } catch (Exception e) {
    msg = e.getMessage();
    }
    return msg;
    }
Deepak
  • 187
  • 1
  • 1
  • 10
Shafin Raza
  • 21
  • 1
  • 8

1 Answers1

0

try out.close(); in this piece of code just like this

try {
out = new FileOutputStream(filename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
   out.close();
}
masoud vali
  • 1,528
  • 2
  • 18
  • 29