Ejemplo n.º 1
0
 private void copyImageToPrivateStorage(File file, Uri image, int sampleSize)
     throws FileCopyException {
   file.getParentFile().mkdirs();
   InputStream is = null;
   OutputStream os = null;
   try {
     if (!file.exists() && !file.createNewFile()) {
       throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
     }
     is = mXmppConnectionService.getContentResolver().openInputStream(image);
     if (is == null) {
       throw new FileCopyException(R.string.error_not_an_image_file);
     }
     Bitmap originalBitmap;
     BitmapFactory.Options options = new BitmapFactory.Options();
     int inSampleSize = (int) Math.pow(2, sampleSize);
     Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
     options.inSampleSize = inSampleSize;
     originalBitmap = BitmapFactory.decodeStream(is, null, options);
     is.close();
     if (originalBitmap == null) {
       throw new FileCopyException(R.string.error_not_an_image_file);
     }
     Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
     int rotation = getRotation(image);
     scaledBitmap = rotate(scaledBitmap, rotation);
     boolean targetSizeReached = false;
     int quality = Config.IMAGE_QUALITY;
     while (!targetSizeReached) {
       os = new FileOutputStream(file);
       boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
       if (!success) {
         throw new FileCopyException(R.string.error_compressing_image);
       }
       os.flush();
       targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50;
       quality -= 5;
     }
     scaledBitmap.recycle();
   } catch (FileNotFoundException e) {
     throw new FileCopyException(R.string.error_file_not_found);
   } catch (IOException e) {
     e.printStackTrace();
     throw new FileCopyException(R.string.error_io_exception);
   } catch (SecurityException e) {
     throw new FileCopyException(R.string.error_security_exception_during_image_copy);
   } catch (OutOfMemoryError e) {
     ++sampleSize;
     if (sampleSize <= 3) {
       copyImageToPrivateStorage(file, image, sampleSize);
     } else {
       throw new FileCopyException(R.string.error_out_of_memory);
     }
   } finally {
     close(os);
     close(is);
   }
 }
Ejemplo n.º 2
0
 public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
   String filename =
       fileDateFormat.format(new Date(message.getTimeSent()))
           + "_"
           + message.getUuid().substring(0, 4);
   switch (Config.IMAGE_FORMAT) {
     case JPEG:
       message.setRelativeFilePath(filename + ".jpg");
       break;
     case PNG:
       message.setRelativeFilePath(filename + ".png");
       break;
     case WEBP:
       message.setRelativeFilePath(filename + ".webp");
       break;
   }
   copyImageToPrivateStorage(getFile(message), image);
   updateFileParams(message);
 }
Ejemplo n.º 3
0
 public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
   Log.d(
       Config.LOGTAG,
       "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
   copyImageToPrivateStorage(file, image, 0);
 }