Checks whether Bitmap data is valid. - Android Graphics

Android examples for Graphics:Bitmap File

Description

Checks whether Bitmap data is valid.

Demo Code

/*/*  w w w .j a v  a2s .  c  o  m*/
 CanvasDrawUtils.java
 Copyright (c) 2015 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
//package com.java2s;

import android.graphics.BitmapFactory;

public class Main {
    /**
     * Defined a size of size.
     */
    private static final int MAX_SIZE = 2048;

    /**
     * Checks whether data is valid.
     *
     * @param data image data
     * @return true if data is valid, false otherwise
     */
    public static boolean checkBitmap(byte[] data) {
        if (data != null) {
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory
                        .decodeByteArray(data, 0, data.length, options);
                if (options.outWidth > MAX_SIZE
                        || options.outHeight > MAX_SIZE) {
                    return false;
                } else {
                    return options.outWidth > 0 && options.outHeight > 0;
                }
            } catch (Throwable t) {
                // format error if an error has occurred
                return false;
            }
        }
        return false;
    }
}

Related Tutorials