Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 package com.android.contacts.util;
     19 
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.graphics.Bitmap;
     23 import android.net.Uri;
     24 import android.os.Environment;
     25 import android.provider.MediaStore;
     26 import android.util.Log;
     27 
     28 import java.io.ByteArrayOutputStream;
     29 import java.io.File;
     30 import java.io.IOException;
     31 import java.text.SimpleDateFormat;
     32 import java.util.Date;
     33 
     34 /**
     35  * Utilities related to loading/saving contact photos.
     36  *
     37  */
     38 public class ContactPhotoUtils {
     39     private static final String TAG = "ContactPhotoUtils";
     40 
     41     private static final String PHOTO_DATE_FORMAT = "'IMG'_yyyyMMdd_HHmmss";
     42     private static final String NEW_PHOTO_DIR_PATH =
     43             Environment.getExternalStorageDirectory() + "/DCIM/Camera";
     44 
     45 
     46     /**
     47      * Generate a new, unique file to be used as an out-of-band communication
     48      * channel, since hi-res Bitmaps are too big to serialize into a Bundle.
     49      * This file will be passed to other activities (such as the gallery/camera/cropper/etc.),
     50      * and read by us once they are finished writing it.
     51      */
     52     public static File generateTempPhotoFile(Context context) {
     53         return new File(pathForCroppedPhoto(context, generateTempPhotoFileName()));
     54     }
     55 
     56     public static String pathForCroppedPhoto(Context context, String fileName) {
     57         final File dir = new File(context.getExternalCacheDir() + "/tmp");
     58         dir.mkdirs();
     59         final File f = new File(dir, fileName);
     60         return f.getAbsolutePath();
     61     }
     62 
     63     public static String pathForNewCameraPhoto(String fileName) {
     64         final File dir = new File(NEW_PHOTO_DIR_PATH);
     65         dir.mkdirs();
     66         final File f = new File(dir, fileName);
     67         return f.getAbsolutePath();
     68     }
     69 
     70     public static String generateTempPhotoFileName() {
     71         Date date = new Date(System.currentTimeMillis());
     72         SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT);
     73         return "ContactPhoto-" + dateFormat.format(date) + ".jpg";
     74     }
     75 
     76     /**
     77      * Creates a byte[] containing the PNG-compressed bitmap, or null if
     78      * something goes wrong.
     79      */
     80     public static byte[] compressBitmap(Bitmap bitmap) {
     81         final int size = bitmap.getWidth() * bitmap.getHeight() * 4;
     82         final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
     83         try {
     84             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
     85             out.flush();
     86             out.close();
     87             return out.toByteArray();
     88         } catch (IOException e) {
     89             Log.w(TAG, "Unable to serialize photo: " + e.toString());
     90             return null;
     91         }
     92     }
     93 
     94     /**
     95      * Adds common extras to gallery intents.
     96      *
     97      * @param intent The intent to add extras to.
     98      * @param croppedPhotoUri The uri of the file to save the image to.
     99      * @param photoSize The size of the photo to scale to.
    100      */
    101     public static void addGalleryIntentExtras(Intent intent, Uri croppedPhotoUri, int photoSize) {
    102         intent.putExtra("crop", "true");
    103         intent.putExtra("scale", true);
    104         intent.putExtra("scaleUpIfNeeded", true);
    105         intent.putExtra("aspectX", 1);
    106         intent.putExtra("aspectY", 1);
    107         intent.putExtra("outputX", photoSize);
    108         intent.putExtra("outputY", photoSize);
    109         intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedPhotoUri);
    110     }
    111 }
    112 
    113 
    114