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.ClipData; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.net.Uri; 26 import android.os.Environment; 27 import android.provider.MediaStore; 28 import android.support.v4.content.FileProvider; 29 import android.util.Log; 30 31 import com.google.common.io.Closeables; 32 33 import java.io.ByteArrayOutputStream; 34 import java.io.File; 35 import java.io.FileNotFoundException; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.text.SimpleDateFormat; 40 import java.util.Date; 41 import java.util.Locale; 42 43 /** 44 * Utilities related to loading/saving contact photos. 45 * 46 */ 47 public class ContactPhotoUtils { 48 private static final String TAG = "ContactPhotoUtils"; 49 50 private static final String PHOTO_DATE_FORMAT = "'IMG'_yyyyMMdd_HHmmss"; 51 52 public static final String FILE_PROVIDER_AUTHORITY = "com.android.contacts.files"; 53 54 /** 55 * Generate a new, unique file to be used as an out-of-band communication 56 * channel, since hi-res Bitmaps are too big to serialize into a Bundle. 57 * This file will be passed (as a uri) to other activities (such as the gallery/camera/ 58 * cropper/etc.), and read by us once they are finished writing it. 59 */ 60 public static Uri generateTempImageUri(Context context) { 61 return FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, 62 new File(pathForTempPhoto(context, generateTempPhotoFileName()))); 63 } 64 65 public static Uri generateTempCroppedImageUri(Context context) { 66 return FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, 67 new File(pathForTempPhoto(context, generateTempCroppedPhotoFileName()))); 68 } 69 70 private static String pathForTempPhoto(Context context, String fileName) { 71 final File dir = context.getCacheDir(); 72 dir.mkdirs(); 73 final File f = new File(dir, fileName); 74 return f.getAbsolutePath(); 75 } 76 77 private static String generateTempPhotoFileName() { 78 final Date date = new Date(System.currentTimeMillis()); 79 SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US); 80 return "ContactPhoto-" + dateFormat.format(date) + ".jpg"; 81 } 82 83 private static String generateTempCroppedPhotoFileName() { 84 final Date date = new Date(System.currentTimeMillis()); 85 SimpleDateFormat dateFormat = new SimpleDateFormat(PHOTO_DATE_FORMAT, Locale.US); 86 return "ContactPhoto-" + dateFormat.format(date) + "-cropped.jpg"; 87 } 88 89 /** 90 * Given a uri pointing to a bitmap, reads it into a bitmap and returns it. 91 * @throws FileNotFoundException 92 */ 93 public static Bitmap getBitmapFromUri(Context context, Uri uri) throws FileNotFoundException { 94 final InputStream imageStream = context.getContentResolver().openInputStream(uri); 95 try { 96 return BitmapFactory.decodeStream(imageStream); 97 } finally { 98 Closeables.closeQuietly(imageStream); 99 } 100 } 101 102 /** 103 * Creates a byte[] containing the PNG-compressed bitmap, or null if 104 * something goes wrong. 105 */ 106 public static byte[] compressBitmap(Bitmap bitmap) { 107 final int size = bitmap.getWidth() * bitmap.getHeight() * 4; 108 final ByteArrayOutputStream out = new ByteArrayOutputStream(size); 109 try { 110 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 111 out.flush(); 112 out.close(); 113 return out.toByteArray(); 114 } catch (IOException e) { 115 Log.w(TAG, "Unable to serialize photo: " + e.toString()); 116 return null; 117 } 118 } 119 120 public static void addCropExtras(Intent intent, int photoSize) { 121 intent.putExtra("crop", "true"); 122 intent.putExtra("scale", true); 123 intent.putExtra("scaleUpIfNeeded", true); 124 intent.putExtra("aspectX", 1); 125 intent.putExtra("aspectY", 1); 126 intent.putExtra("outputX", photoSize); 127 intent.putExtra("outputY", photoSize); 128 } 129 130 /** 131 * Adds common extras to gallery intents. 132 * 133 * @param intent The intent to add extras to. 134 * @param photoUri The uri of the file to save the image to. 135 */ 136 public static void addPhotoPickerExtras(Intent intent, Uri photoUri) { 137 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); 138 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | 139 Intent.FLAG_GRANT_READ_URI_PERMISSION); 140 intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, photoUri)); 141 } 142 143 /** 144 * Given an input photo stored in a uri, save it to a destination uri 145 */ 146 public static boolean savePhotoFromUriToUri(Context context, Uri inputUri, Uri outputUri, 147 boolean deleteAfterSave) { 148 FileOutputStream outputStream = null; 149 InputStream inputStream = null; 150 try { 151 outputStream = context.getContentResolver() 152 .openAssetFileDescriptor(outputUri, "rw").createOutputStream(); 153 inputStream = context.getContentResolver().openInputStream( 154 inputUri); 155 156 final byte[] buffer = new byte[16 * 1024]; 157 int length; 158 int totalLength = 0; 159 while ((length = inputStream.read(buffer)) > 0) { 160 outputStream.write(buffer, 0, length); 161 totalLength += length; 162 } 163 Log.v(TAG, "Wrote " + totalLength + " bytes for photo " + inputUri.toString()); 164 } catch (IOException e) { 165 Log.e(TAG, "Failed to write photo: " + inputUri.toString() + " because: " + e); 166 return false; 167 } finally { 168 Closeables.closeQuietly(inputStream); 169 Closeables.closeQuietly(outputStream); 170 if (deleteAfterSave) { 171 context.getContentResolver().delete(inputUri, null, null); 172 } 173 } 174 return true; 175 } 176 } 177 178 179