Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.graphics.*;

import android.net.Uri;

import java.io.InputStream;

public class Main {
    public static final boolean checkImageAndSize(Context context, Uri uri, long bytes) {
        if (uri == null)
            return false;

        try {
            InputStream is = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options imageOptions = new BitmapFactory.Options();
            imageOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, imageOptions);
            //android.util.Log.d("test77", "Original Image Size: " + imageOptions.outWidth + " x " + imageOptions.outHeight);
            is.close();
            if (imageOptions.outWidth <= 0 || imageOptions.outHeight <= 0) {
                return false;
            }
            is = context.getContentResolver().openInputStream(uri);
            long fileSizeCounter = 0;
            long size;
            byte[] buf = new byte[8192];
            while ((size = is.read(buf, 0, buf.length)) != -1) {
                //android.util.Log.d("test77", "size="+size);
                fileSizeCounter += size;
            }
            is.close();
            if (fileSizeCounter > bytes) {
                return false;
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}