create Thumbnail From EXIF - Android Graphics

Android examples for Graphics:Bitmap Thumbnail

Description

create Thumbnail From EXIF

Demo Code

/*/* w w w  .  j  ava  2s . co m*/
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.OutputStream;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.BaseColumns;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;

public class Main{
    private static final String TAG = "ThumbnailUtil";
    static byte[] createThumbnailFromEXIF(String filePath, int targetSize) {
        if (filePath == null) {
            return null;
        }

        try {
            final ExifInterface exif = new ExifInterface(filePath);
            if (exif == null) {
                return null;
            }
            final byte[] thumbData = exif.getThumbnail();
            if (thumbData == null) {
                return null;
            }
            // Sniff the size of the EXIF thumbnail before decoding it. Photos
            // from the device will pass, but images that are side loaded from
            // other cameras may not.
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length,
                    options);

            final int width = options.outWidth;
            final int height = options.outHeight;

            if (width >= targetSize && height >= targetSize) {
                return thumbData;
            }
        } catch (final IOException ex) {
            Log.w(TAG, ex);
        }
        return null;
    }
}

Related Tutorials