• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.camera.session;
18 
19 import android.content.ContentResolver;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.location.Location;
23 import android.net.Uri;
24 
25 import com.android.camera.Storage;
26 import com.android.camera.debug.Log;
27 
28 import java.io.File;
29 
30 /**
31  * Default implementation of the {@link StackSaver} interface. It creates a
32  * directory for each stack and stores images and if needed also metadata inside
33  * that directory.
34  * <p>
35  * TODO: Add placeholder support for stack writing.
36  * </p>
37  */
38 public class StackSaverImpl implements StackSaver {
39     private static final Log.Tag TAG = new Log.Tag("StackSaverImpl");
40     /** The stacked images are stored in this directory. */
41     private final File mStackDirectory;
42     private final ContentResolver mContentResolver;
43     private final Location mGpsLocation;
44 
45     /**
46      * Instantiate a new stack saver implementation.
47      *
48      * @param stackDirectory the directory, which either exists already or can
49      *            be created, into which images belonging to this stack are
50      *            belonging.
51      * @param gpsLocation the GPS location to attach to all stacked images.
52      * @param contentResolver content resolver for storing the data in media
53      *            store. TODO: Replace with a media storage storer that can be
54      *            mocked out in tests.
55      */
StackSaverImpl(File stackDirectory, Location gpsLocation, ContentResolver contentResolver)56     public StackSaverImpl(File stackDirectory, Location gpsLocation,
57             ContentResolver contentResolver) {
58         mStackDirectory = stackDirectory;
59         mGpsLocation = gpsLocation;
60         mContentResolver = contentResolver;
61     }
62 
63     @Override
saveStackedImage(File inputImagePath, String title, int width, int height, int imageOrientation, long captureTimeEpoch, String mimeType)64     public Uri saveStackedImage(File inputImagePath, String title, int width, int height,
65             int imageOrientation, long captureTimeEpoch, String mimeType) {
66         String filePath =
67                 Storage.instance().generateFilepath(mStackDirectory.getAbsolutePath(),
68                 title, mimeType);
69         Log.d(TAG, "Saving using stack image saver: " + filePath);
70         File outputImagePath = new File(filePath);
71         Bitmap bitmap = BitmapFactory.decodeFile(filePath);
72 
73         if (Storage.instance().renameFile(inputImagePath, outputImagePath)) {
74             long fileLength = outputImagePath.length();
75             if (fileLength > 0) {
76                 return Storage.instance().addImageToMediaStore(mContentResolver, title,
77                         captureTimeEpoch, mGpsLocation, imageOrientation, fileLength, bitmap,
78                         width, height, mimeType, null);
79             }
80         }
81 
82         Log.e(TAG, String.format("Unable to rename file from %s to %s.",
83                 inputImagePath.getPath(),
84                 filePath));
85         return null;
86     }
87 }
88