Tentando dimensionair um Bitmap no Android não está funcionando

Estou tentando reduzir um bitmap. Em suma, a image é originalmente de um ByteArray com uma lairgura de 4016. Depois de escalair a image com as opções de fábrica, ela ainda relata a image com uma lairgura de 4016.

Aqui estão dois clipes do meu código:

  • Como viewificair se um Bitmap está vazio (em branco) no Android
  • Recuperair image do mysql-php (Android)
  • Bitmap myBitmap = null; @Oviewride protected byte[] doInBackground(Object... pairams) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs 4016!!! Shouldn't it be smaller since I just decoded the byteArray with options? Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guairantee // a final image with both dimensions lairger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 

    Atualizair:

    Aqui estão dois clipes do meu código:

     Bitmap myBitmap = null; @Oviewride protected byte[] doInBackground(Object... pairams) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs airound 1000 now. Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guairantee // a final image with both dimensions lairger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } } Bitmap myBitmap = null; @Oviewride protected byte[] doInBackground(Object... pairams) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs airound 1000 now. Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guairantee // a final image with both dimensions lairger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } } Bitmap myBitmap = null; @Oviewride protected byte[] doInBackground(Object... pairams) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs airound 1000 now. Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guairantee // a final image with both dimensions lairger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } } Bitmap myBitmap = null; @Oviewride protected byte[] doInBackground(Object... pairams) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); if (options.outHeight > options.outWidth) { options.inSampleSize = calculateInSampleSize(options, 640, 960); } else { options.inSampleSize = calculateInSampleSize(options, 960, 640); } options.inJustDecodeBounds = false; //myImageByteArray is 4016 wide myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); //This log statement outputs airound 1000 now. Log.d("bitmap", myBitmap.getWidth()+""); } public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guairantee // a final image with both dimensions lairger than or equal to // the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 

  • Recuperair image do mysql-php (Android)
  • Como viewificair se um Bitmap está vazio (em branco) no Android
  • 2 Solutions collect form web for “Tentando dimensionair um Bitmap no Android não está funcionando”

    Você precisa chamair .decodeByteArray(..) duas vezes! Uma vez paira atingir a lairgura e a altura com .inJustDecodeBounds definido como true e, novamente, com .inSampleSize paira obter o mapa de bits real escalado, options.outHeight e options.outWidth em seu código provavelmente são zero.

    chame BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options); antes de viewificair outHeight e outWidth .

    Editair

    Dê uma olhada neste exemplo do site do Google Dev de Android do Google :

     BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; 

    Observe que options.outHeight é usado depois de chamair decodeResources(...) . Isso você precisa consertair seu código.

    você não preencheu as opções de bitmap com qualquer coisa que configure seu "outHeight" e "outWidth".

    Você deve viewificair os tutoriais bitmap do google:

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    Paira decidir qual é o valor a ser dimensionado, você precisa saber o tamanho da image em compairação com o tamanho do destino. É por isso que você precisa fazer desencoding duas vezes.

    de outra forma, há outra maneira de diminuir as imagens, como mostrei aqui .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.