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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;

import android.text.TextUtils;

public class Main {

    private static InputStream bitmap2InputStream(Context context, Bitmap bitmap) throws IOException {
        File file = new File(context.getFilesDir(), "wallpaper.jpg");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
        fileOutputStream.close();
        return filePath2InputStream(file.getAbsolutePath());
    }

    private static InputStream filePath2InputStream(String filePath) throws IOException {
        if (TextUtils.isEmpty(filePath)) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        return new FileInputStream(file);
    }
}