Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    private static final String TAG = "BitmapUtility";
    public static Bitmap bitmapResult = null;

    public static Bitmap loadBitmap(String url) {
        Log.d(TAG, "Start Load Url : " + url);
        //Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new URL(url).openStream());
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[16384];

            while ((nRead = in.read(data, 0, data.length)) != -1) {
                dataStream.write(data, 0, nRead);
            }
            dataStream.flush();
            final byte[] newData = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options);
        } catch (IOException e) {
            Log.e("My fault", "Could not load Bitmap from: " + url);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmapResult;
    }
}