Search in sources :

Example 11 with ExifInterface

use of android.support.media.ExifInterface in project AnExplorer by 1hakr.

the class DocumentsContract method openImageThumbnail.

/**
 * Open the given image for thumbnail purposes, using any embedded EXIF
 * thumbnail if available, and providing orientation hints from the parent
 * image.
 *
 * @hide
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static AssetFileDescriptor openImageThumbnail(File file) throws FileNotFoundException {
    final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    Bundle extras = null;
    if (!Utils.hasKitKat()) {
        return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
    }
    try {
        final ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                extras = new Bundle(1);
                extras.putInt(EXTRA_ORIENTATION, 270);
                break;
        }
        final long[] thumb = exif.getThumbnailRange();
        if (thumb != null) {
            return new AssetFileDescriptor(pfd, thumb[0], thumb[1], extras);
        }
    } catch (IOException e) {
        CrashReportingManager.logException(e);
    }
    return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH, extras);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) ExifInterface(android.support.media.ExifInterface) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) TargetApi(android.annotation.TargetApi)

Example 12 with ExifInterface

use of android.support.media.ExifInterface in project AnExplorer by 1hakr.

the class DocumentArchive method openDocumentThumbnail.

/**
 * Opens a thumbnail of a file within an archive.
 */
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, final CancellationSignal signal) throws FileNotFoundException {
    final ParsedDocumentId parsedId = ParsedDocumentId.fromDocumentId(documentId, mIdDelimiter);
    Preconditions.checkArgumentEquals(mDocumentId, parsedId.mArchiveId, "Mismatching document ID. Expected: %s, actual: %s.");
    Preconditions.checkArgumentNotNull(parsedId.mPath, "Not a document within an archive.");
    Preconditions.checkArgument(getDocumentType(documentId).startsWith("image/"), "Thumbnails only supported for image/* MIME type.");
    final ZipEntry entry = mEntries.get(parsedId.mPath);
    if (entry == null) {
        throw new FileNotFoundException();
    }
    if (!Utils.hasKitKat()) {
        return new AssetFileDescriptor(openDocument(documentId, "r", signal), 0, entry.getSize());
    }
    InputStream inputStream = null;
    try {
        inputStream = mZipFile.getInputStream(entry);
        final ExifInterface exif = new ExifInterface(inputStream);
        if (exif.hasThumbnail()) {
            Bundle extras = null;
            switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 270);
                    break;
            }
            final long[] thumb = exif.getThumbnailRange();
            if (thumb != null) {
                return new AssetFileDescriptor(openDocument(documentId, "r", signal), thumb[0], thumb[1], extras);
            }
        }
    } catch (IOException e) {
        // Ignore the exception, as reading the EXIF may legally fail.
        Log.e(TAG, "Failed to obtain thumbnail from EXIF.", e);
        CrashReportingManager.logException(e);
    } finally {
        IoUtils.closeQuietly(inputStream);
    }
    return new AssetFileDescriptor(openDocument(documentId, "r", signal), 0, entry.getSize(), null);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) Bundle(android.os.Bundle) ZipEntry(java.util.zip.ZipEntry) ExifInterface(android.support.media.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 13 with ExifInterface

use of android.support.media.ExifInterface in project Zom-Android by zom.

the class SecureMediaStore method getThumbnailFile.

public static Bitmap getThumbnailFile(Context context, Uri uri, int thumbnailSize) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inInputShareable = true;
    options.inPurgeable = true;
    BitmapFactory.decodeStream(is, null, options);
    if ((options.outWidth == -1) || (options.outHeight == -1))
        return null;
    int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
    is.close();
    is = context.getContentResolver().openInputStream(uri);
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = calculateInSampleSize(options, thumbnailSize, thumbnailSize);
    Bitmap scaledBitmap = BitmapFactory.decodeStream(is, null, opts);
    is.close();
    ExifInterface exif = new ExifInterface(context.getContentResolver().openInputStream(uri));
    int orientationType = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    int orientationD = 0;
    if (orientationType == ExifInterface.ORIENTATION_ROTATE_90)
        orientationD = 90;
    else if (orientationType == ExifInterface.ORIENTATION_ROTATE_180)
        orientationD = 180;
    else if (orientationType == ExifInterface.ORIENTATION_ROTATE_270)
        orientationD = 270;
    if (orientationD != 0)
        scaledBitmap = rotateBitmap(scaledBitmap, orientationD);
    return scaledBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) FileInputStream(info.guardianproject.iocipher.FileInputStream) InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory)

Example 14 with ExifInterface

use of android.support.media.ExifInterface in project Zom-Android by zom.

the class SecureMediaStore method getImageOrientation.

public static int getImageOrientation(String imagePath) {
    int rotate = 0;
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rotate;
}
Also used : ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException)

Example 15 with ExifInterface

use of android.support.media.ExifInterface in project muzei by romannurik.

the class RealRenderController method openDownloadedCurrentArtwork.

@Override
protected BitmapRegionLoader openDownloadedCurrentArtwork(boolean forceReload) {
    // Load the stream
    try {
        // Check if there's rotation
        int rotation = 0;
        try (InputStream in = mContext.getContentResolver().openInputStream(MuzeiContract.Artwork.CONTENT_URI)) {
            if (in == null) {
                return null;
            }
            ExifInterface exifInterface = new ExifInterface(in);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotation = 270;
                    break;
            }
        } catch (IOException | StackOverflowError e) {
            Log.w(TAG, "Couldn't open EXIF interface on artwork", e);
        }
        return BitmapRegionLoader.newInstance(mContext.getContentResolver().openInputStream(MuzeiContract.Artwork.CONTENT_URI), rotation);
    } catch (IOException e) {
        Log.e(TAG, "Error loading image", e);
        return null;
    }
}
Also used : InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException)

Aggregations

ExifInterface (android.support.media.ExifInterface)26 IOException (java.io.IOException)21 InputStream (java.io.InputStream)12 Bitmap (android.graphics.Bitmap)4 Date (java.util.Date)4 BitmapFactory (android.graphics.BitmapFactory)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ContentResolver (android.content.ContentResolver)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 Resources (android.content.res.Resources)2 Cursor (android.database.Cursor)2 Matrix (android.graphics.Matrix)2 Point (android.graphics.Point)2 Uri (android.net.Uri)2 Bundle (android.os.Bundle)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Locale (java.util.Locale)2