Android Open Source - GreenerGasPedal Bitmap Util






From Project

Back to project page GreenerGasPedal.

License

The source code is released under:

GNU General Public License

If you think the Android project GreenerGasPedal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package name.nanek.greenerpedal.activity.support;
//w w w. j  a  v  a 2 s  .  c o  m
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Environment;
import android.util.Log;
import android.view.View;

public class BitmapUtil {

  private static final String LOG_TAG = BitmapUtil.class.getSimpleName();

  private static final boolean PRETEND_NO_SD = false;

  private static final String IMAGE_OUTPUT_FILE_NAME = "greener_pedal.jpg";

  
  /**
   * Draws view to a bitmap image.
   */
  public static Bitmap drawToBitmap(final View viewToDraw) {
    
    final Bitmap bitmap = Bitmap.createBitmap(viewToDraw.getWidth(),
        viewToDraw.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    viewToDraw.draw(canvas);
    return bitmap;
  }
  
  public static String saveImageAndRecycleBitmap(final Context context, Bitmap bitmap) {
    OutputStream outputStream = null;

    try {
      final File targetFile;
      final File externalStorageDirectory = Environment
          .getExternalStorageDirectory();
      boolean useSdCard = !PRETEND_NO_SD
          && externalStorageDirectory.exists();
      if (useSdCard) {
        targetFile = new File(externalStorageDirectory,
            IMAGE_OUTPUT_FILE_NAME);
        if (targetFile.exists()) {
          targetFile.delete();
        }
        outputStream = new FileOutputStream(targetFile);

      } else {
        targetFile = context.getFileStreamPath(IMAGE_OUTPUT_FILE_NAME);
        outputStream = context.openFileOutput(IMAGE_OUTPUT_FILE_NAME, Context.MODE_WORLD_READABLE);
      }

      bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
      bitmap.recycle();
      bitmap = null;
      System.gc();
      System.gc();

      outputStream.flush();
      outputStream.close();

      // MediaStore.Images.Media.insertImage(getContentResolver(),
      // targetFile.getAbsolutePath(), targetFile.getName()
      // ,targetFile.getName());

      return targetFile.getAbsolutePath();

    } catch (final Exception e) {
      Log.e(LOG_TAG, "Error saving image.", e);
    } finally {
      if (null != bitmap) {
        bitmap.recycle();
        System.gc();
        System.gc();
      }
      if (null != outputStream) {
        try {
          outputStream.close();
        } catch (IOException e) {
          // Do nothing.
        }
      }
    }
    
    return null;
  }
  
}




Java Source Code List

name.nanek.greenerpedal.GreenerPedalApp.java
name.nanek.greenerpedal.activity.DisplayReadingsActivity.java
name.nanek.greenerpedal.activity.EditPrefsActivity.java
name.nanek.greenerpedal.activity.GraphAndShareActivity.java
name.nanek.greenerpedal.activity.support.APISafeKeyguardHider.java
name.nanek.greenerpedal.activity.support.BitmapUtil.java
name.nanek.greenerpedal.activity.support.LogLine.java
name.nanek.greenerpedal.activity.support.LowPassFilter.java
name.nanek.greenerpedal.activity.support.MessageOnlyFormatter.java
name.nanek.greenerpedal.activity.support.Prefs.java
name.nanek.greenerpedal.activity.support.ScreenLock.java
name.nanek.greenerpedal.activity.support.Sensors.java
name.nanek.greenerpedal.activity.support.ShareUtil.java
name.nanek.greenerpedal.activity.support.Trigger.java
name.nanek.greenerpedal.activity.support.TriggersResult.java
name.nanek.greenerpedal.activity.support.Views.java
name.nanek.greenerpedal.db.model.Reading.java
name.nanek.greenerpedal.db.model.dao.DataHelper.java
name.nanek.greenerpedal.db.model.dao.ReadingDao.java
name.nanek.greenerpedal.db.model.dao.support.DateUtil.java
name.nanek.greenerpedal.db.model.dao.support.DbUtil.java