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.graphics.Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static final String FileSavePath = "/mnt/sdcard/BoYue/";

    public static boolean writeBitmap(Bitmap b) {
        String fn = "logo_qrcode.png";
        ByteArrayOutputStream by = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.PNG, 100, by);
        byte[] stream = by.toByteArray();
        return writeToSdcard(stream, FileSavePath, fn);
    }

    public static boolean writeToSdcard(byte[] data, String path, String fileName) {
        FileOutputStream fos = null;
        try {
            File filePath = new File(path);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            File file = new File(path + fileName);
            if (file.exists()) {
                file.delete();
            }
            fos = new FileOutputStream(file);
            fos.write(data);
            fos.flush();
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}