Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static boolean convertBytes2Bitmap(String imageName, byte[] byt) {
        if (byt.length == 0)
            return false;
        boolean success = true;
        Bitmap bmp = BitmapFactory.decodeByteArray(byt, 0, byt.length);
        File file = new File("/sdcard/" + imageName + ".png");

        try {
            file.createNewFile();
        } catch (IOException e) {
            success = false;
        }

        FileOutputStream out = null;

        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            success = false;
        }

        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

        try {
            out.flush();
        } catch (IOException e) {
            success = false;
        }

        try {
            out.close();
        } catch (IOException e) {
        }

        return success;
    }
}