scrollview Content to Png Image - Android Graphics

Android examples for Graphics:PNG Image

Description

scrollview Content to Png Image

Demo Code


//package com.java2s;

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

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;

import android.net.Uri;
import android.os.Environment;

import android.widget.ScrollView;

public class Main {
    public static String DCIMCamera_PATH = Environment
            .getExternalStorageDirectory() + "/DCIM/Camera/";

    /**//  www .  j a va 2s.  com
     * mScrollView
     * 
     * @param context
     * @param scrollView
     */
    public static void scrollviewContent2Png(Context context,
            ScrollView scrollView) {
        Bitmap bmp = null;
        bmp = getBitmapByView(scrollView);
        // new Thread(new WorkThread(bmp)).start();
        saveBitmapToCamera(context, bmp, null);
    }

    public static Bitmap getBitmapByView(ScrollView scrollView) {
        int h = 0;
        Bitmap bitmap = null;

        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
            scrollView.getChildAt(i).setBackgroundColor(
                    Color.parseColor("#ffffff"));
        }

        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    }

    public static Boolean saveBitmapToCamera(Context context, Bitmap bm,
            String name) {

        File file = null;

        if (name == null || name.equals("")) {
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            Date curDate = new Date(System.currentTimeMillis());
            name = formatter.format(curDate) + ".jpg";
        }

        file = new File(DCIMCamera_PATH, name);
        if (file.exists()) {
            file.delete();
        }

        try {
            FileOutputStream out = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;

        } catch (IOException e) {

            e.printStackTrace();
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        context.sendBroadcast(intent);

        return true;
    }
}

Related Tutorials