Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;

public class Main {
    static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w,
            int h) {
        if (str_url == null || str_url.length() <= 0)
            return null;

        BitmapDrawable thumb = null;
        try {
            URL url = new URL(str_url);
            URLConnection connection = url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            BufferedInputStream data = new BufferedInputStream(stream);

            Bitmap bitmap = BitmapFactory.decodeStream(data);
            if (bitmap != null) {
                if (scale)
                    thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true));
                else
                    thumb = new BitmapDrawable(resources, bitmap);
            }

        } catch (MalformedURLException e) {
            //e.printStackTrace();
        } catch (IOException e) {
            //e.printStackTrace();
        }

        return thumb;
    }
}