@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) { InputStream is = null; try { is = new FileInputStream(imageFileLocation); } catch (FileNotFoundException e) { e.printStackTrace(); } if (is == null) { try { Uri u = data.getData(); is = getContentResolver().openInputStream(u); } catch (FileNotFoundException e) { e.printStackTrace(); } } try { Options opt = new BitmapFactory.Options(); opt.inTempStorage = new byte[16 * 1024]; opt.inSampleSize = 6; Bitmap imageBitmap = BitmapFactory.decodeStream(is, new Rect(), opt); ByteArrayOutputStream bao = new ByteArrayOutputStream(); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao); System.out.println(bao.size()); imgData = new byte[bao.size()]; imgData = bao.toByteArray(); if (is != null) { is.close(); } if (bao != null) { bao.close(); } } catch (IOException e) { e.printStackTrace(); } getSharedPreferences(); getLocation(); if (isOnline()) { new UploadTask().execute(); } else { _sinDTO.setChannelId(_journey.getChannelId()); _sinDTO.setLongitude(String.valueOf(lng)); _sinDTO.setLatitude(String.valueOf(lat)); _sinDTO.setPicData(imgData); insertedId = dbs.insert(_sinDTO); showMessage("Foto tersimpan di local, karena tidak terdapat koneksi internet"); } } }
private void copyOptions(Options srcOptions, Options destOptions) { destOptions.inDensity = srcOptions.inDensity; destOptions.inDither = srcOptions.inDither; destOptions.inInputShareable = srcOptions.inInputShareable; destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds; destOptions.inPreferredConfig = srcOptions.inPreferredConfig; destOptions.inPurgeable = srcOptions.inPurgeable; destOptions.inSampleSize = srcOptions.inSampleSize; destOptions.inScaled = srcOptions.inScaled; destOptions.inScreenDensity = srcOptions.inScreenDensity; destOptions.inTargetDensity = srcOptions.inTargetDensity; destOptions.inTempStorage = srcOptions.inTempStorage; if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions); if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions); }
public static Bitmap resizeBitMapImage1( String filePath, int targetWidth, int targetHeight, int orientation) { Log.v("RHIMAGE Orientation", String.valueOf(orientation)); Bitmap bitMapImage = null; // First, get the dimensions of the image Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Reads options for the file double sampleSize = 0; // Only scale if we need to // (16384 buffer for img processing) // Switched inequality to scale by opposite dimension Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) <= Math.abs(options.outWidth - targetWidth); // Load, scaling to smallest power of 2 that'll get it <= desired // dimensions if (options.outHeight * options.outWidth * 2 >= 1638) { sampleSize = scaleByHeight ? options.outHeight / targetHeight : options.outWidth / targetWidth; sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d))); } // Do the actual decoding options.inJustDecodeBounds = false; options.inTempStorage = new byte[128]; while (true) { try { options.inSampleSize = (int) sampleSize; bitMapImage = BitmapFactory.decodeFile(filePath, options); // The funny thing here is that just doing orientation * 90 doesn't // seem to work as you might expect - it actually flips the image // also, originally we didn't multiply by 90 and didn't have crashes, // so either the orientation value has changed from 360 degrees to -1,0,1 // or this didn't cause an error in the past because we didn't realize // orientation could have a negative value. This needs to be tested on // a variety of devices. switch (orientation) { case -1: Matrix matrix = new Matrix(); matrix.postRotate(90); bitMapImage = Bitmap.createBitmap( bitMapImage, 0, 0, bitMapImage.getWidth(), bitMapImage.getHeight(), matrix, true); break; default: // no nothing break; } break; } catch (Exception ex) { try { sampleSize = sampleSize * 2; } catch (Exception ex1) { } } } // and crop int originx = 0; int originy = 0; int width = bitMapImage.getWidth(); int height = bitMapImage.getHeight(); float ratio = (float) targetWidth / (float) targetHeight; float reverseRatio = (float) targetHeight / (float) targetWidth; // use smaller dimension if (width > height) { targetHeight = height; targetWidth = (int) (targetHeight * ratio); } else { targetWidth = width; targetHeight = (int) (targetWidth * reverseRatio); } // adjust back when the outputs are too big if (targetWidth > width) { targetWidth = width; } if (targetHeight > height) { targetHeight = height; } originx = (bitMapImage.getWidth() - targetWidth) / 2; originy = (bitMapImage.getHeight() - targetHeight) / 2; // Log.v(TAG, Integer.toString() ) bitMapImage = Bitmap.createBitmap(bitMapImage, originx, originy, targetWidth, targetHeight); return bitMapImage; }