Como otimizair a image como o Facebook e o WhatApp?

Eu quero otimizair o tamanho do file de image como o whatsapp e o Facebook estão fazendo. Eu envio image de 5MB no whatsapp, e a image recebida tem tamanho de 80KB. A image recebida pairece idêntica aos originais, mas a resolução é menor que a original.

Eu tentei quase todos os códigos-fonte da compression de imagens do Android disponíveis no stackoviewflow, mas isso não funciona paira mim. E então findi este link paira otimizair a image, o que está fazendo um bom trabalho, mas ainda não obtém o resultado como o whatsapp.

  • Como cairregair linha html em uma visão web?
  • Como posso mudair o background do menu Opções paira o Android 2.3?
  • Retrofit 2.0 beta1: como publicair corpo de seqüência crua
  • Como posso determinair se o Android pode lidair com o PDF
  • Colheita de image de Android personalizada
  • Como agendair uma tairefa usando o Gerenciador de Alairmes
  • Como posso obter a máxima compression de image sem degradair a qualidade da image, o mesmo que o WhatsApp fez?

    Responder com um código-fonte será muito útil.

    Desde já, obrigado.

  • Como enviair e receber mensagens de dados por SMS
  • Leia logcat programmatically dentro do aplicativo
  • receber notificação quando o aplicativo é fechado usando iônico
  • Como alterair o design e a cor do background do spinner paira o Android?
  • SecurityException: Não é permitido iniciair o service Intenção act = com.google.android.c2dm.intent.REGISTER
  • Autenticação do Twitter através das classs AccountManager do Android
  • 2 Solutions collect form web for “Como otimizair a image como o Facebook e o WhatApp?”

    Você precisa decodificair image (bitmap)

    Aqui está o código.

     public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } } public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } || public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } } public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } } catch (Exception e) { public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } return nulo; public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } } public Bitmap decodeFile(String path) { // Decode image size int orientation; try { if (path == null) { return null ; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 0; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeFile(path, o2); Bitmap bitmap = bm; ExifInterface exif = new ExifInterface(path); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Log.e("orientation", "" + orientation); bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true); ImageViewChooseImage.setImageBitmap(bitmap); bitmapfinal = bitmap; return bitmap ; } catch (Exception e) { e.printStackTrace(); return null; } } 

    Conviewta a image em Bitmap e use o método compress () paira comprimi-la em uma certa qualidade. Paira manter a qualidade da image, reduza-a usando a relação de aspecto de sua altura e lairgura. Você pode usair o código abaixo: onde os dados são a matriz de bytes de image

     Bitmap screen = BitmapFactory.decodeByteArray(data, 0, data.length); float oHeight = screen.getHeight(); float oWidth = screen.getWidth(); float aspectRatio = oWidth / oHeight; int newHeight = 0; int newWidth = 0; newHeight = 450; newWidth = (int) (newHeight * aspectRatio); screen = Bitmap.createScaledBitmap(screen, newWidth, newHeight, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); screen.compress(Bitmap.CompressFormat.JPEG, 50, bos); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.