Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen an image from Uri, resize and return Base64
    primarykey
    data
    text
    <p>I'm trying to open an image from a specific uri, checking if this image is too big to resize it, and return it as Base64. My problem is the code I have to resize image never resize images (never indicate that the image is too big). I looked for others questions similar this, but I didn't get any answer. I don't know why always in this two code lines the values are -1:</p> <pre><code> final int height = options.outHeight; // Always -1 WHY? final int width = options.outWidth; // Always -1 WHY? </code></pre> <p>I attach my code:</p> <pre><code>private class WriteImage extends AsyncTask&lt;Object, Void, String&gt;{ private static Bitmap decodeSampledBitmapFromStream(InputStream is, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; InputStream aux = inputStreamCopy(is); BitmapFactory.decodeStream(is, null, options); // SkImageDecoder::Factory returned null try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(aux, null, options); } private static InputStream inputStreamCopy(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; byte[] buffer = new byte[1024]; try { while ((len = is.read(buffer)) &gt; -1) baos.write(buffer, 0, len); } catch (IOException e) { e.printStackTrace(); } return new ByteArrayInputStream(baos.toByteArray()); } private static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; // Always -1 WHY? final int width = options.outWidth; // Always -1 WHY? int inSampleSize = 1; if (height &gt; reqHeight || width &gt; reqWidth) { if (width &gt; height) inSampleSize = Math.round((float)height / (float)reqHeight); else inSampleSize = Math.round((float)width / (float)reqWidth); // This offers some additional logic in case the image has a strange // aspect ratio. For example, a panorama may have a much larger // width than height. In these cases the total pixels might still // end up being too large to fit comfortably in memory, so we should // be more aggressive with sample down the image (=larger // inSampleSize). final float totalPixels = width * height; // Anything more than 2x the requested pixels we'll sample down // further. final float totalReqPixelsCap = reqWidth * reqHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) &gt; totalReqPixelsCap) inSampleSize++; } for (int i=5; i&gt;0; i--){ if (Math.pow(2, i)&lt;=inSampleSize){ inSampleSize = (int) Math.pow(2, i); break; } } return inSampleSize; } @Override protected void onPreExecute(){ } @Override protected void onProgressUpdate(Void... values){ } @Override protected String doInBackground(Object... params) { try { InputStream is = ((android.app.Activity) params[1]).getContentResolver().openInputStream((Uri) params[0]); Bitmap bitmap = decodeSampledBitmapFromStream(is, 300, 300); int bytes = bitmap.getWidth()*bitmap.getHeight()*4; ByteBuffer buffer = ByteBuffer.allocate(bytes); bitmap.copyPixelsToBuffer(buffer); return Base64.encodeToString(buffer.array(), Base64.DEFAULT); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result){ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload