Search in sources :

Example 21 with ExifInterface

use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.

the class DateTakenRetriever method getExifDateTaken.

private static long getExifDateTaken(Context context, AlbumItem albumItem) {
    String mimeType = MediaType.getMimeType(context, albumItem.getUri(context));
    if (MediaType.doesSupportExifMimeType(mimeType)) {
        ExifInterface exif = ExifUtil.getExifInterface(context, albumItem);
        if (exif != null) {
            String dateTakenString = String.valueOf(ExifUtil.getCastValue(exif, ExifInterface.TAG_DATETIME));
            if (dateTakenString != null && !dateTakenString.equals("null")) {
                Locale locale = Util.getLocale(context);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", locale);
                try {
                    Date dateTaken = sdf.parse(dateTakenString);
                    return dateTaken.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return -1;
}
Also used : Locale(java.util.Locale) ExifInterface(android.support.media.ExifInterface) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 22 with ExifInterface

use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.

the class ExifUtil method saveExifData.

public static void saveExifData(String path, ExifItem[] exifData) {
    try {
        ExifInterface exif = new ExifInterface(path);
        for (int i = 0; i < exifData.length; i++) {
            ExifItem exifItem = exifData[i];
            exif.setAttribute(exifItem.getTag(), exifItem.getValue());
        }
        exif.saveAttributes();
    } catch (IOException | IllegalArgumentException e) {
        e.printStackTrace();
    }
}
Also used : ExifInterface(android.support.media.ExifInterface) IOException(java.io.IOException)

Example 23 with ExifInterface

use of android.support.media.ExifInterface in project Camera-Roll-Android-App by kollerlukas.

the class ExifUtil method retrieveExifData.

public static ExifItem[] retrieveExifData(Context context, Uri uri) {
    ExifInterface exif = getExifInterface(context, AlbumItem.getInstance(context, uri));
    if (exif != null) {
        String[] exifTags = getExifTags();
        ExifItem[] exifData = new ExifItem[exifTags.length];
        for (int i = 0; i < exifTags.length; i++) {
            String tag = exifTags[i];
            String value = exif.getAttribute(tag);
            ExifItem exifItem = new ExifItem(tag, value);
            exifData[i] = exifItem;
        }
        return exifData;
    }
    return null;
}
Also used : ExifInterface(android.support.media.ExifInterface)

Example 24 with ExifInterface

use of android.support.media.ExifInterface in project Pix-Art-Messenger by kriztan.

the class ConversationFragment method attachImageToConversation.

private void attachImageToConversation(Conversation conversation, Uri uri, boolean sendAsIs) {
    if (conversation == null) {
        return;
    }
    if (sendAsIs) {
        sendImage(conversation, uri);
        return;
    }
    final Conversation conversation_preview = conversation;
    final Uri uri_preview = uri;
    Bitmap bitmap = BitmapFactory.decodeFile(FileUtils.getPath(activity, uri));
    File file = null;
    ExifInterface exif = null;
    int orientation = 0;
    try {
        file = new File(FileUtils.getPath(activity, uri));
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (file != null) {
        try {
            exif = new ExifInterface(file.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    }
    Log.d(Config.LOGTAG, "EXIF: " + orientation);
    Bitmap rotated_image = null;
    Log.d(Config.LOGTAG, "Rotate image");
    rotated_image = FileBackend.rotateBitmap(file, bitmap, orientation);
    if (rotated_image != null) {
        int scaleSize = 600;
        int originalWidth = rotated_image.getWidth();
        int originalHeight = rotated_image.getHeight();
        int newWidth = -1;
        int newHeight = -1;
        float multFactor;
        if (originalHeight > originalWidth) {
            newHeight = scaleSize;
            multFactor = (float) originalWidth / (float) originalHeight;
            newWidth = (int) (newHeight * multFactor);
        } else if (originalWidth > originalHeight) {
            newWidth = scaleSize;
            multFactor = (float) originalHeight / (float) originalWidth;
            newHeight = (int) (newWidth * multFactor);
        } else if (originalHeight == originalWidth) {
            newHeight = scaleSize;
            newWidth = scaleSize;
        }
        Log.d(Config.LOGTAG, "Scaling preview image from " + originalHeight + "px x " + originalWidth + "px to " + newHeight + "px x " + newWidth + "px");
        Bitmap preview = Bitmap.createScaledBitmap(rotated_image, newWidth, newHeight, false);
        ImageView ImagePreview = new ImageView(activity);
        LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        ImagePreview.setLayoutParams(vp);
        ImagePreview.setMaxWidth(newWidth);
        ImagePreview.setMaxHeight(newHeight);
        // ImagePreview.setScaleType(ImageView.ScaleType.FIT_XY);
        // ImagePreview.setAdjustViewBounds(true);
        ImagePreview.setPadding(5, 5, 5, 5);
        ImagePreview.setImageBitmap(preview);
        getActivity().runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setView(ImagePreview);
            builder.setTitle(R.string.send_image);
            builder.setPositiveButton(R.string.ok, (dialog, which) -> sendImage(conversation_preview, uri_preview));
            builder.setOnCancelListener(dialog -> mPendingImageUris.clear());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                builder.setOnDismissListener(dialog -> mPendingImageUris.clear());
            }
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        });
    } else {
        getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), getText(R.string.error_file_not_found), Toast.LENGTH_LONG).show());
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) ExifInterface(android.support.media.ExifInterface) Conversation(de.pixart.messenger.entities.Conversation) IOException(java.io.IOException) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) IOException(java.io.IOException) SendIntentException(android.content.IntentSender.SendIntentException) ActivityNotFoundException(android.content.ActivityNotFoundException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Bitmap(android.graphics.Bitmap) ImageView(android.widget.ImageView) File(java.io.File) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) LinearLayout(android.widget.LinearLayout)

Example 25 with ExifInterface

use of android.support.media.ExifInterface in project iNaturalistAndroid by inaturalist.

the class ObservationEditor method importPhotoMetadata.

private void importPhotoMetadata(Uri photoUri) {
    try {
        InputStream is = getContentResolver().openInputStream(photoUri);
        ExifInterface exif = new ExifInterface(is);
        float[] latLng = new float[2];
        uiToObservation();
        if (exif.getLatLong(latLng)) {
            stopGetLocation();
            mObservation.latitude = (double) latLng[0];
            mObservation.longitude = (double) latLng[1];
            mObservation.positional_accuracy = null;
            if ((mObservation.geoprivacy != null) && ((mObservation.geoprivacy.equals("private") || mObservation.geoprivacy.equals("obscured")))) {
                mObservation.private_longitude = mObservation.longitude;
                mObservation.private_latitude = mObservation.latitude;
            }
            if (mObservation.latitude_changed()) {
                if (isNetworkAvailable()) {
                    guessLocation();
                } else {
                    setPlaceGuess(null);
                }
            }
        } else {
        // No coordinates - don't override the observation coordinates
        }
        String datetime;
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            // Older Android (before v7), doesn't parse the EXIF tag of TAG_DATETIME_ORIGINAL
            datetime = exif.getAttribute(ExifInterface.TAG_DATETIME);
        } else {
            datetime = exif.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL);
        }
        if (datetime != null) {
            SimpleDateFormat exifDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            try {
                Date date = exifDateFormat.parse(datetime);
                Timestamp timestamp = new Timestamp(date.getTime());
                mObservation.observed_on = timestamp;
                mObservation.time_observed_at = timestamp;
                mObservation.observed_on_string = mApp.formatDatetime(timestamp);
                mObservedOnStringTextView.setText(mApp.formatDatetime(timestamp));
                mObservedOnStringTextView.setTextColor(Color.parseColor("#000000"));
                mTimeObservedAtButton.setText(mApp.shortFormatTime(timestamp));
                mTimeObservedAtButton.setTextColor(Color.parseColor("#000000"));
                mDateSetByUser = timestamp;
                mTimeSetByUser = timestamp;
            } catch (ParseException e) {
                Log.d(TAG, "Failed to parse " + datetime + ": " + e);
            }
        } else {
            // No original datetime - nullify the date
            mObservation.observed_on = null;
            mObservation.time_observed_at = null;
            mObservation.observed_on_string = null;
        }
        is.close();
        observationToUi();
    } catch (IOException e) {
        Log.e(TAG, "couldn't find " + photoUri);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExifInterface(android.support.media.ExifInterface) ParseException(java.text.ParseException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date)

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