urbanairship-core/src/main/java/com/urbanairship/util/BitmapUtils.java
@Nullable
    public static Bitmap fetchScaledBitmap(@NonNull Context context, @NonNull URL url, int reqWidth, int reqHeight) throws IOException {
        Logger.verbose("BitmapUtils - Fetching image from: " + url);

        boolean deleteFile = false;
        File imageFile = null;
        if (URLUtil.isFileUrl(url.toString())) {
            deleteFile = false;
            try {
                imageFile = new File(url.toURI());
            } catch (URISyntaxException e) {
                Logger.error("BitmapUtils - Invalid URL: " + url);
            }
        }

        if (imageFile == null) {
            imageFile = File.createTempFile("ua_", ".temp", context.getCacheDir());
            deleteFile = true;
            Logger.verbose("BitmapUtils - Created temp file: " + imageFile);

            if (!FileUtils.downloadFile(url, imageFile).isSuccess) {
                Logger.verbose("BitmapUtils - Failed to fetch image from: " + url);
                return null;
            }
        }


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);

        int width = options.outWidth;
        int height = options.outHeight;

        options.inSampleSize = calculateInSampleSize(width, height, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);

        if (deleteFile) {
            if (imageFile.delete()) {
                Logger.verbose("BitmapUtils - Deleted temp file: " + imageFile);
            } else {
                Logger.verbose("BitmapUtils - Failed to delete temp file: " + imageFile);
            }
        }

        if (bitmap == null) {
            Logger.error("BitmapUtils - Failed to create bitmap for URL: " + url);
            return null;
        }

        Logger.debug(String.format(Locale.US, "BitmapUtils - Fetched image from: %s. Original image size: %dx%d. Requested image size: %dx%d. Bitmap size: %dx%d. SampleSize: %d",
                url, width, height, reqWidth, reqHeight, bitmap.getWidth(), bitmap.getHeight(), options.inSampleSize));

        return bitmap;
    }
/**
     * Create a scaled bitmap.
     *
     * @param context The application context.
     * @param url The URL image.
     * @param reqWidth The requested width of the image.
     * @param reqHeight The requested height of the image.
     * @return The scaled bitmap.
     * @throws IOException
     */
Create a scaled bitmap.
@param context The application context.
@param url The URL image.
@param reqWidth The requested width of the image.
@param reqHeight The requested height of the image.
@return The scaled bitmap.
Create a scaled bitmap . @param context The application context . @param url The URL image . @param reqWidth The requested width of the image . @param reqHeight The requested height of the image . @return The scaled bitmap .
@Nullable
    public static Bitmap fetchScaledBitmap(@NonNull Context context, @NonNull URL url, int reqWidth, int reqHeight) throws IOException {
        Logger.verbose("BitmapUtils - Fetching image from: " + url);
        boolean deleteFile = false;
        File imageFile = null;
        if (URLUtil.isFileUrl(url.toString())) {
            deleteFile = false;
            try {
                imageFile = new File(url.toURI());
            } catch (URISyntaxException e) {
                Logger.error("BitmapUtils - Invalid URL: " + url);
            }
        }
        if (imageFile == null) {
            imageFile = File.createTempFile("ua_", ".temp", context.getCacheDir());
            deleteFile = true;
            Logger.verbose("BitmapUtils - Created temp file: " + imageFile);
            if (!FileUtils.downloadFile(url, imageFile).isSuccess) {
                Logger.verbose("BitmapUtils - Failed to fetch image from: " + url);
                return null;
            }
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        int width = options.outWidth;
        int height = options.outHeight;
        options.inSampleSize = calculateInSampleSize(width, height, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        if (deleteFile) {
            if (imageFile.delete()) {
                Logger.verbose("BitmapUtils - Deleted temp file: " + imageFile);
            } else {
                Logger.verbose("BitmapUtils - Failed to delete temp file: " + imageFile);
            }
        }
        if (bitmap == null) {
            Logger.error("BitmapUtils - Failed to create bitmap for URL: " + url);
            return null;
        }
        Logger.debug(String.format(Locale.US, "BitmapUtils - Fetched image from: %s. Original image size: %dx%d. Requested image size: %dx%d. Bitmap size: %dx%d. SampleSize: %d",
                url, width, height, reqWidth, reqHeight, bitmap.getWidth(), bitmap.getHeight(), options.inSampleSize));
        return bitmap;
    }
@ Nullable public static Bitmap fetchScaledBitmap ( @ NonNull Context context , @ NonNull URL url , int reqWidth , int reqHeight ) throws IOException { Logger . verbose ( "BitmapUtils - Fetching image from: " + url ) ; boolean deleteFile = false ; File imageFile = null ; if ( URLUtil . isFileUrl ( url . toString ( ) ) ) { deleteFile = false ; try { imageFile = new File ( url . toURI ( ) ) ; } catch ( URISyntaxException e ) { Logger . error ( "BitmapUtils - Invalid URL: " + url ) ; } } if ( imageFile == null ) { imageFile = File . createTempFile ( "ua_" , ".temp" , context . getCacheDir ( ) ) ; deleteFile = true ; Logger . verbose ( "BitmapUtils - Created temp file: " + imageFile ) ; if ( ! FileUtils . downloadFile ( url , imageFile ) . isSuccess ) { Logger . verbose ( "BitmapUtils - Failed to fetch image from: " + url ) ; return null ; } } BitmapFactory . Options options = new BitmapFactory . Options ( ) ; options . inJustDecodeBounds = true ; BitmapFactory . decodeFile ( imageFile . getAbsolutePath ( ) , options ) ; int width = options . outWidth ; int height = options . outHeight ; options . inSampleSize = calculateInSampleSize ( width , height , reqWidth , reqHeight ) ; options . inJustDecodeBounds = false ; Bitmap bitmap = BitmapFactory . decodeFile ( imageFile . getAbsolutePath ( ) , options ) ; if ( deleteFile ) { if ( imageFile . delete ( ) ) { Logger . verbose ( "BitmapUtils - Deleted temp file: " + imageFile ) ; } else { Logger . verbose ( "BitmapUtils - Failed to delete temp file: " + imageFile ) ; } } if ( bitmap == null ) { Logger . error ( "BitmapUtils - Failed to create bitmap for URL: " + url ) ; return null ; } Logger . debug ( String . format ( Locale . US , "BitmapUtils - Fetched image from: %s. Original image size: %dx%d. Requested image size: %dx%d. Bitmap size: %dx%d. SampleSize: %d" , url , width , height , reqWidth , reqHeight , bitmap . getWidth ( ) , bitmap . getHeight ( ) , options . inSampleSize ) ) ; return bitmap ; }
Create a scaled bitmap.
Create a scaled bitmap .