예제 #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);
   }
 }
예제 #2
0
 public Bitmap cropCenterSquare(Uri image, int size) {
   if (image == null) {
     return null;
   }
   InputStream is = null;
   try {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inSampleSize = calcSampleSize(image, size);
     is = mXmppConnectionService.getContentResolver().openInputStream(image);
     if (is == null) {
       return null;
     }
     Bitmap input = BitmapFactory.decodeStream(is, null, options);
     if (input == null) {
       return null;
     } else {
       input = rotate(input, getRotation(image));
       return cropCenterSquare(input, size);
     }
   } catch (SecurityException e) {
     return null; // happens for example on Android 6.0 if contacts permissions get revoked
   } catch (FileNotFoundException e) {
     return null;
   } finally {
     close(is);
   }
 }
예제 #3
0
 public boolean save(Avatar avatar) {
   File file;
   if (isAvatarCached(avatar)) {
     file = new File(getAvatarPath(avatar.getFilename()));
   } else {
     String filename = getAvatarPath(avatar.getFilename());
     file = new File(filename + ".tmp");
     file.getParentFile().mkdirs();
     OutputStream os = null;
     try {
       file.createNewFile();
       os = new FileOutputStream(file);
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       digest.reset();
       DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
       mDigestOutputStream.write(avatar.getImageAsBytes());
       mDigestOutputStream.flush();
       mDigestOutputStream.close();
       String sha1sum = CryptoHelper.bytesToHex(digest.digest());
       if (sha1sum.equals(avatar.sha1sum)) {
         file.renameTo(new File(filename));
       } else {
         Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
         file.delete();
         return false;
       }
     } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
       return false;
     } finally {
       close(os);
     }
   }
   avatar.size = file.length();
   return true;
 }
예제 #4
0
 private int getRotation(Uri image) {
   InputStream is = null;
   try {
     is = mXmppConnectionService.getContentResolver().openInputStream(image);
     return ExifHelper.getOrientation(is);
   } catch (FileNotFoundException e) {
     return 0;
   } finally {
     close(is);
   }
 }
예제 #5
0
 public void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
   Log.d(
       Config.LOGTAG,
       "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
   file.getParentFile().mkdirs();
   OutputStream os = null;
   InputStream is = null;
   try {
     file.createNewFile();
     os = new FileOutputStream(file);
     is = mXmppConnectionService.getContentResolver().openInputStream(uri);
     byte[] buffer = new byte[1024];
     int length;
     while ((length = is.read(buffer)) > 0) {
       try {
         os.write(buffer, 0, length);
       } catch (IOException e) {
         throw new FileWriterException();
       }
     }
     try {
       os.flush();
     } catch (IOException e) {
       throw new FileWriterException();
     }
   } catch (FileNotFoundException e) {
     throw new FileCopyException(R.string.error_file_not_found);
   } catch (FileWriterException e) {
     throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
   } catch (IOException e) {
     e.printStackTrace();
     throw new FileCopyException(R.string.error_io_exception);
   } finally {
     close(os);
     close(is);
   }
 }
예제 #6
0
  public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
    if (image == null) {
      return null;
    }
    InputStream is = null;
    try {
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
      is = mXmppConnectionService.getContentResolver().openInputStream(image);
      if (is == null) {
        return null;
      }
      Bitmap source = BitmapFactory.decodeStream(is, null, options);
      if (source == null) {
        return null;
      }
      int sourceWidth = source.getWidth();
      int sourceHeight = source.getHeight();
      float xScale = (float) newWidth / sourceWidth;
      float yScale = (float) newHeight / sourceHeight;
      float scale = Math.max(xScale, yScale);
      float scaledWidth = scale * sourceWidth;
      float scaledHeight = scale * sourceHeight;
      float left = (newWidth - scaledWidth) / 2;
      float top = (newHeight - scaledHeight) / 2;

      RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
      Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(dest);
      canvas.drawBitmap(source, null, targetRect, null);
      if (source != null && !source.isRecycled()) {
        source.recycle();
      }
      return dest;
    } catch (SecurityException e) {
      return null; // android 6.0 with revoked permissions for example
    } catch (FileNotFoundException e) {
      return null;
    } finally {
      close(is);
    }
  }
예제 #7
0
 public Avatar getStoredPepAvatar(String hash) {
   if (hash == null) {
     return null;
   }
   Avatar avatar = new Avatar();
   File file = new File(getAvatarPath(hash));
   FileInputStream is = null;
   try {
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(file.getAbsolutePath(), options);
     is = new FileInputStream(file);
     ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
     Base64OutputStream mBase64OutputStream =
         new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
     MessageDigest digest = MessageDigest.getInstance("SHA-1");
     DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
     byte[] buffer = new byte[4096];
     int length;
     while ((length = is.read(buffer)) > 0) {
       os.write(buffer, 0, length);
     }
     os.flush();
     os.close();
     avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
     avatar.image = new String(mByteArrayOutputStream.toByteArray());
     avatar.height = options.outHeight;
     avatar.width = options.outWidth;
     return avatar;
   } catch (IOException e) {
     return null;
   } catch (NoSuchAlgorithmException e) {
     return null;
   } finally {
     close(is);
   }
 }