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 import java.util.Locale; 34 35 /** 36 * Utilities related to loading/saving contact photos. 37 * 38 */ 39 public class ContactPhotoUtils { 40 private static final String TAG = "ContactPhotoUtils"; 41 42 private static final String PHOTO_DATE_FORMAT = "'IMG'_yyyyMMdd_HHmmss"; 43 private static final String NEW_PHOTO_DIR_PATH = 44 Environment.getExternalStorageDirectory() + "/DCIM/Camera"; 45 46 47 /** 48 * Generate a new, unique file to be used as an out-of-band communication 49 * channel, since hi-res Bitmaps are too big to serialize into a Bundle. 50 * This file will be passed to other activities (such as the gallery/camera/cropper/etc.), 51 * and read by us once they are finished writing it. 52 */ 53 public static File generateTempPhotoFile(Context context) { 54 return new File(pathForCroppedPhoto(context, generateTempPhotoFileName())); 55 } 56 57 public static String pathForCroppedPhoto(Context context, String fileName) { 58 final File dir = new File(context.getExternalCacheDir() + "/tmp"); 59 dir.mkdirs(); 60 final File f = new File(dir, fileName); 61 return f.getAbsolutePath(); 62 } 63 64 public static String pathForNewCameraPhoto(String fileName) { 65 final File dir = new File(NEW_PHOTO_DIR_PATH); 66 dir.mkdirs(); 67 final File f = new File(dir, fileName); 68 return f.getAbsolutePath(); 69 } 70 71 public static String generateTempPhotoFileName() { 72 Date date = new Date(System.currentTimeMillis()); 73 SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US); 74 return "ContactPhoto-" + dateFormat.format(date) + ".jpg"; 75 } 76 77 /** 78 * Creates a byte[] containing the PNG-compressed bitmap, or null if 79 * something goes wrong. 80 */ 81 public static byte[] compressBitmap(Bitmap bitmap) { 82 final int size = bitmap.getWidth() * bitmap.getHeight() * 4; 83 final ByteArrayOutputStream out = new ByteArrayOutputStream(size); 84 try { 85 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 86 out.flush(); 87 out.close(); 88 return out.toByteArray(); 89 } catch (IOException e) { 90 Log.w(TAG, "Unable to serialize photo: " + e.toString()); 91 return null; 92 } 93 } 94 95 /** 96 * Adds common extras to gallery intents. 97 * 98 * @param intent The intent to add extras to. 99 * @param croppedPhotoUri The uri of the file to save the image to. 100 * @param photoSize The size of the photo to scale to. 101 */ 102 public static void addGalleryIntentExtras(Intent intent, Uri croppedPhotoUri, int photoSize) { 103 intent.putExtra("crop", "true"); 104 intent.putExtra("scale", true); 105 intent.putExtra("scaleUpIfNeeded", true); 106 intent.putExtra("aspectX", 1); 107 intent.putExtra("aspectY", 1); 108 intent.putExtra("outputX", photoSize); 109 intent.putExtra("outputY", photoSize); 110 intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedPhotoUri); 111 } 112 } 113 114 115