Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

public class Main {
    public static void setFBImage(final String fbid, final Context context, final ImageView v) {
        new AsyncTask<String, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(String... strings) {
                File img = new File(context.getFilesDir() + "/profile.jpg");
                Bitmap bmp = null;
                if (img.exists()) {
                    try {
                        bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
                    } catch (Exception E) {
                        E.printStackTrace();
                    }
                } else {
                    try {
                        URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid)
                                + "/picture?type=large&redirect=true&width=400&height=400");
                        bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                        FileOutputStream fOut = new FileOutputStream(img);
                        bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                        fOut.flush();
                        fOut.close();
                    } catch (Exception E) {
                        E.printStackTrace();
                    }
                }
                return bmp;
            }

            @Override
            protected void onPostExecute(Bitmap img) {
                if (img != null) {
                    v.setImageBitmap(img);
                }
            }
        }.execute();
    }
}