android.support.media.ExifInterface Java Examples

The following examples show how to use android.support.media.ExifInterface. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static void saveChanges(final ExifInterface exifInterface,
                               final List<ExifItem> editedItems, final Callback callback) {
    if (exifInterface == null) {
        return;
    }
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            boolean success = true;
            for (ExifItem item : editedItems) {
                exifInterface.setAttribute(item.getTag(), item.getValue());
            }
            try {
                exifInterface.saveAttributes();
            } catch (IOException e) {
                e.printStackTrace();
                success = false;
            }

            if (callback != null) {
                callback.done(success);
            }
        }
    });
}
 
Example #2
Source File: SecureMediaStore.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: Util.java    From CVScanner with GNU General Public License v3.0 6 votes vote down vote up
public static int getExifRotation(Context context, Uri imageUri) throws IOException {
    if (imageUri == null) return 0;
    InputStream inputStream = null;
    try {
        inputStream = context.getContentResolver().openInputStream(imageUri);
        ExifInterface exifInterface = new ExifInterface(inputStream);
        // We only recognize a subset of orientation tag values
        switch (exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return 270;
            default:
                return ExifInterface.ORIENTATION_UNDEFINED;
        }
    }finally {
        closeSilently(inputStream);
    }
}
 
Example #4
Source File: ImageUtil.java    From Learning-Resources with MIT License 6 votes vote down vote up
/**
 * Does the same as {@link #isPictureValidForUpload(Uri)} but takes a file path instead of an
 * Uri.
 *
 * @param filePath The path to the image.
 * @throws IOException Thrown if the file cannot be found.
 */
public boolean isPictureValidForUpload(final String filePath) throws IOException {
    BitmapFactory.Options opts = getImageDimensions(filePath);
    ExifInterface exif = new ExifInterface(filePath);
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_NORMAL);

    // Translate the orientation constant to degrees.
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            orientation = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            orientation = 270;
            break;

        default:
            break;
    }

    return isPictureDimensionsValid(opts.outWidth, opts.outHeight, orientation);
}
 
Example #5
Source File: PostProcessor.java    From camerakit-android with MIT License 6 votes vote down vote up
public boolean areDimensionsFlipped() {
    switch (orientation) {
        case ExifInterface.ORIENTATION_TRANSPOSE:
        case ExifInterface.ORIENTATION_ROTATE_90:
        case ExifInterface.ORIENTATION_TRANSVERSE:
        case ExifInterface.ORIENTATION_ROTATE_270:
            return true;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        case ExifInterface.ORIENTATION_ROTATE_180:
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        case ExifInterface.ORIENTATION_NORMAL:
        case ExifInterface.ORIENTATION_UNDEFINED:
            return false;
    }

    return false;
}
 
Example #6
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static LocationItem retrieveLocation(Context context, ExifInterface exif) {
    if (exif != null) {
        Object latitudeObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_GPS_LATITUDE);
        Object longitudeObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_GPS_LONGITUDE);
        if (latitudeObject != null && longitudeObject != null) {
            boolean positiveLat = ExifUtil.getCastValue(exif, ExifInterface.TAG_GPS_LATITUDE_REF).equals("N");
            double latitude = Double.parseDouble(Parser.parseGPSLongOrLat(String.valueOf(latitudeObject), positiveLat));

            boolean positiveLong = ExifUtil.getCastValue(exif, ExifInterface.TAG_GPS_LONGITUDE_REF).equals("E");
            double longitude = Double.parseDouble(Parser.parseGPSLongOrLat(String.valueOf(longitudeObject), positiveLong));
            String locationString = latitude + "," + longitude;

            return new LocationItem(context.getString(R.string.info_location), locationString);
        }
    }
    return new LocationItem(context.getString(R.string.info_location), ExifUtil.NO_DATA);
}
 
Example #7
Source File: DateTakenRetriever.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static void removeExifData(final ExifInterface exifInterface, final Callback callback) {
    if (exifInterface == null) {
        return;
    }
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            boolean success = true;
            // remove all Exif data
            for (String tag : valuesToRemove) {
                exifInterface.setAttribute(tag, null);
            }
            try {
                exifInterface.saveAttributes();
            } catch (IOException e) {
                e.printStackTrace();
                success = false;
            }

            if (callback != null) {
                callback.done(success);
            }
        }
    });
}
 
Example #9
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static int getExifOrientationAngle(Context context, AlbumItem albumItem) {
    ExifInterface exif = getExifInterface(context, albumItem);
    if (exif == null) {
        return 0;
    }
    int orientation = (int) getCastValue(exif, ExifInterface.TAG_ORIENTATION);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        default:
            return 0;
    }
}
 
Example #10
Source File: BitmapUtils.java    From giffun with Apache License 2.0 6 votes vote down vote up
/**
 * Rotate the given image by given Exif value.<br>
 * If no rotation is required the image will not be rotated.<br>
 * New bitmap is created and the old one is recycled.
 */
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
  int degrees;
  int orientation =
      exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
      degrees = 90;
      break;
    case ExifInterface.ORIENTATION_ROTATE_180:
      degrees = 180;
      break;
    case ExifInterface.ORIENTATION_ROTATE_270:
      degrees = 270;
      break;
    default:
      degrees = 0;
      break;
  }
  return new RotateBitmapResult(bitmap, degrees);
}
 
Example #11
Source File: TagSettings.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public int getCameraPhotoOrientation(Uri file){
    int rotate = 0;
    try (InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(file)) {
        ExifInterface exif = new ExifInterface(inputStream);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        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 (Exception e) {
        Log.e(TAG, "Could not get orientation of image");
    }
    return rotate;
}
 
Example #12
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveDimensions(Context context, ExifInterface exif, AlbumItem albumItem) {
    if (exif != null) {
        String height = String.valueOf(ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_LENGTH));
        String width = String.valueOf(ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_WIDTH));
        return new InfoItem(context.getString(R.string.info_dimensions), width + " x " + height);
    }
    /*Exif not supported/working for this image*/
    int[] imageDimens = albumItem.getImageDimens(context);
    return new InfoItem(context.getString(R.string.info_dimensions),
            String.valueOf(imageDimens[0]) + " x " + String.valueOf(imageDimens[1]));
}
 
Example #13
Source File: Util.java    From CVScanner with GNU General Public License v3.0 5 votes vote down vote up
public static boolean setExifRotation(Context context, Uri imageUri, int rotation) throws IOException {
    if (imageUri == null) return false;

    InputStream destStream = null;
    try{
        destStream = context.getContentResolver().openInputStream(imageUri);

        ExifInterface exif = new ExifInterface(destStream);

        exif.setAttribute("UserComment", "Generated using CVScanner");

        int orientation = ExifInterface.ORIENTATION_NORMAL;
        switch (rotation){
            case 1:
                orientation = ExifInterface.ORIENTATION_ROTATE_90;
                break;

            case 2:
                orientation = ExifInterface.ORIENTATION_ROTATE_180;
                break;

            case 3:
                orientation = ExifInterface.ORIENTATION_ROTATE_270;
                break;
        }
        exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(orientation));
        exif.saveAttributes();
    }finally {
        closeSilently(destStream);
    }
    return true;
}
 
Example #14
Source File: CropImageView.java    From giffun with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a Bitmap and initializes the image rotation according to the EXIT data.<br>
 * <br>
 * The EXIF can be retrieved by doing the following: <code>
 * ExifInterface exif = new ExifInterface(path);</code>
 *
 * @param bitmap the original bitmap to set; if null, this
 * @param exif the EXIF information about this bitmap; may be null
 */
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {
  Bitmap setBitmap;
  int degreesRotated = 0;
  if (bitmap != null && exif != null) {
    BitmapUtils.RotateBitmapResult result = BitmapUtils.rotateBitmapByExif(bitmap, exif);
    setBitmap = result.bitmap;
    degreesRotated = result.degrees;
    mInitialDegreesRotated = result.degrees;
  } else {
    setBitmap = bitmap;
  }
  mCropOverlayView.setInitialCropWindowRect(null);
  setBitmap(setBitmap, 0, null, 1, degreesRotated);
}
 
Example #15
Source File: PostProcessor.java    From camerakit-android with MIT License 5 votes vote down vote up
public void apply(JpegTransformer transformer) {
    switch (orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            transformer.flipHorizontal();
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            transformer.rotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            transformer.flipVertical();
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            transformer.rotate(90);
            transformer.flipHorizontal();
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            transformer.rotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            transformer.rotate(270);
            transformer.flipHorizontal();
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            transformer.rotate(90);
            break;
        case ExifInterface.ORIENTATION_NORMAL:
        case ExifInterface.ORIENTATION_UNDEFINED:
            break;
    }
}
 
Example #16
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveISO(Context context, ExifInterface exif) {
    Object isoObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY);
    String iso;
    if (isoObject != null) {
        iso = String.valueOf(isoObject);
    } else {
        iso = ExifUtil.NO_DATA;
    }
    return new InfoItem(context.getString(R.string.info_iso), iso);
}
 
Example #17
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveAperture(Context context, ExifInterface exif) {
    Object apertureObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_F_NUMBER);
    String aperture;
    if (apertureObject != null) {
        aperture = "ƒ/" + String.valueOf(apertureObject);
    } else {
        aperture = ExifUtil.NO_DATA;
    }
    return new InfoItem(context.getString(R.string.info_aperture), aperture);
}
 
Example #18
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveModelAndMake(Context context, ExifInterface exif) {
    Object makeObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_MAKE);
    Object modelObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_MODEL);
    String model;
    if (makeObject != null && modelObject != null) {
        model = String.valueOf(makeObject) + " " + String.valueOf(modelObject);
    } else {
        model = ExifUtil.NO_DATA;
    }
    return new InfoItem(context.getString(R.string.info_camera_model), model);
}
 
Example #19
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveExposure(Context context, ExifInterface exif) {
    Object exposureObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_EXPOSURE_TIME);
    String exposure;
    if (exposureObject != null) {
        exposure = Parser.parseExposureTime(String.valueOf(exposureObject));
    } else {
        exposure = ExifUtil.NO_DATA;
    }
    return new InfoItem(context.getString(R.string.info_exposure), exposure);
}
 
Example #20
Source File: InfoUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static InfoItem retrieveFocalLength(Context context, ExifInterface exif) {
    Object focalLengthObject = ExifUtil.getCastValue(exif, ExifInterface.TAG_FOCAL_LENGTH);
    String focalLength;
    if (focalLengthObject != null) {
        focalLength = String.valueOf(focalLengthObject);
        Rational r = Rational.parseRational(focalLength);
        if (r != null) {
            focalLength = String.valueOf(r.floatValue()) + " mm";
        }
    } else {
        focalLength = ExifUtil.NO_DATA;
    }
    return new InfoItem(context.getString(R.string.info_focal_length), focalLength);
}
 
Example #21
Source File: MainActivity.java    From ActivityDiary with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        if(mCurrentPhotoPath != null && viewModel.getCurrentDiaryUri() != null) {
            Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                    BuildConfig.APPLICATION_ID + ".fileprovider",
                    new File(mCurrentPhotoPath));
            ContentValues values = new ContentValues();
            values.put(ActivityDiaryContract.DiaryImage.URI, photoURI.toString());
            values.put(ActivityDiaryContract.DiaryImage.DIARY_ID, viewModel.getCurrentDiaryUri().getLastPathSegment());

            mQHandler.startInsert(0,
                    null,
                    ActivityDiaryContract.DiaryImage.CONTENT_URI,
                    values);

            if(PreferenceManager
                    .getDefaultSharedPreferences(ActivityDiaryApplication.getAppContext())
                    .getBoolean(SettingsActivity.KEY_PREF_TAG_IMAGES, true)) {
                try {
                    ExifInterface exifInterface = new ExifInterface(mCurrentPhotoPath);
                    if (viewModel.currentActivity().getValue() != null) {
                        /* TODO: #24: when using hierarchical activities tag them all here, seperated with comma */
                        /* would be great to use IPTC keywords instead of EXIF UserComment, but
                         * at time of writing (2017-11-24) it is hard to find a library able to write IPTC
                         * to JPEG for android.
                         * pixymeta-android or apache/commons-imaging could be interesting for this.
                         * */
                        exifInterface.setAttribute(ExifInterface.TAG_USER_COMMENT, viewModel.currentActivity().getValue().getName());
                        exifInterface.saveAttributes();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "writing exif data to " + mCurrentPhotoPath + " failed", e);
                }
            }
        }
    }
}
 
Example #22
Source File: Util.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static int[] getImageDimensions(Context context, Uri uri) {
    int[] dimensions = new int[]{0, 0};

    try {
        InputStream is = context.getContentResolver().openInputStream(uri);

        //try exif
        String mimeType = MediaType.getMimeType(context, uri);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && MediaType.doesSupportExifMimeType(mimeType)
                && is != null) {
            ExifInterface exif = new ExifInterface(is);
            if (exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH) != null
                    && exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH) != null) {
                int width = (int) ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_WIDTH);
                int height = (int) ExifUtil.getCastValue(exif, ExifInterface.TAG_IMAGE_LENGTH);
                if (width != 0 && height != 0) {
                    return new int[]{width, height};
                }
            }
        }

        //exif didn't work
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, new Rect(0, 0, 0, 0), options);
        dimensions[0] = options.outWidth;
        dimensions[1] = options.outHeight;

        if (is != null) {
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return dimensions;
}
 
Example #23
Source File: CustomRotateDimenTransformation.java    From android-slideshow with MIT License 5 votes vote down vote up
/**
 * Calculates the necessary degrees by which the image needs to be rotated in order to be displayed correctly according to the EXIf information.
 *
 * Note: image flipping is not supported, although part of the same EXIF tag
 *
 * @return the degrees to rotate
 */
public static int getRotationFromExif(String filename) {
	try {
		ExifInterface exif = new ExifInterface(filename);
		int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
		Log.d(TAG, "File " + filename + " has EXIF orientation " + orientation);

		return EXIF_ORIENTATION_TO_ROTATION[orientation];
	} catch (IOException e) {
		Log.e(TAG, "EXIF data for file " + filename + " failed to load.");
		return -1;
	}
}
 
Example #24
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #25
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: ExifUtil.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
public static Object getCastValue(ExifInterface exif, String tag)
        throws NumberFormatException, NullPointerException {
    String value = exif.getAttribute(tag);
    return castValue(tag, value);
}
 
Example #27
Source File: JPEGMediaMetadataRetriever.java    From MediaMetadataRetrieverCompat with MIT License 4 votes vote down vote up
@Override
protected int getHeight() {
    return mExif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
}
 
Example #28
Source File: SecureMediaStore.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
public static Bitmap getThumbnailFile(Context context, Uri uri, int thumbnailSize) throws IOException {

        InputStream is = openInputStream(context, 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 = openInputStream(context, uri);

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = calculateInSampleSize(options, thumbnailSize, thumbnailSize);

        Bitmap scaledBitmap = BitmapFactory.decodeStream(is, null, opts);
        is.close();

        InputStream isEx = openInputStream(context,uri);
        ExifInterface exif = new ExifInterface(isEx);
        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);

        isEx.close();

        return scaledBitmap;
    }
 
Example #29
Source File: FileOperation.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
@SuppressLint("ShowToast")
private static void scanPaths(final Context context, final String[] paths, final MediaScannerCallback callback, final boolean withNotification) {
    Log.i("FileOperation", "scanPaths(), paths: " + Arrays.toString(paths));
    if (paths == null) {
        if (callback != null) {
            callback.onAllPathsScanned();
        }
        return;
    }

    //create notification
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createNotificationChannel(context);
    }
    final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context,
            context.getString(R.string.file_op_channel_id))
            .setContentTitle("Scanning...");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notifBuilder.setSmallIcon(R.drawable.ic_autorenew_white);
    }
    notifBuilder.setProgress(paths.length, 0, false);
    final NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < paths.length; i++) {
                String path = paths[i];
                if (MediaType.isMedia(path)) {
                    Uri contentUri = MediaStore.Files.getContentUri("external");
                    ContentResolver resolver = context.getContentResolver();
                    if (new File(path).exists()) {
                        AlbumItem albumItem = AlbumItem.getInstance(path);
                        ContentValues values = new ContentValues();
                        if (albumItem instanceof Video) {
                            values.put(MediaStore.Video.Media.DATA, path);
                            values.put(MediaStore.Video.Media.MIME_TYPE, MediaType.getMimeType(path));
                        } else {
                            values.put(MediaStore.Images.Media.DATA, path);
                            values.put(MediaStore.Images.Media.MIME_TYPE, MediaType.getMimeType(path));
                            try {
                                ExifInterface exif = new ExifInterface(path);
                                Locale locale = us.koller.cameraroll.util.Util.getLocale(context);
                                String dateString = String.valueOf(ExifUtil.getCastValue(exif, ExifInterface.TAG_DATETIME));
                                try {
                                    Date date = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", locale).parse(dateString);
                                    long dateTaken = date.getTime();
                                    values.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken);
                                } catch (ParseException ignored) {
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        resolver.insert(contentUri, values);
                    } else {
                        resolver.delete(contentUri,
                                MediaStore.MediaColumns.DATA + "='" + path + "'",
                                null);
                    }
                }

                if (withNotification) {
                    notifBuilder.setProgress(paths.length, i, false);
                    if (manager != null) {
                        manager.notify(NOTIFICATION_ID, notifBuilder.build());
                    }
                }

            }

            if (manager != null) {
                manager.cancel(NOTIFICATION_ID);
            }

            if (callback != null) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onAllPathsScanned();
                    }
                });
            }
        }
    });
}
 
Example #30
Source File: ImageUpdater.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 13) {
            PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        orientation = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        orientation = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        orientation = 270;
                        break;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            final ArrayList<Object> arrayList = new ArrayList<>();
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false));
            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, 1, new PhotoViewer.EmptyPhotoViewerProvider() {
                @Override
                public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo) {
                    String path = null;
                    MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
                    if (photoEntry.imagePath != null) {
                        path = photoEntry.imagePath;
                    } else if (photoEntry.path != null) {
                        path = photoEntry.path;
                    }
                    Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
                    processBitmap(bitmap);
                }

                @Override
                public boolean allowCaption() {
                    return false;
                }

                @Override
                public boolean canScrollAway() {
                    return false;
                }
            }, null);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 14) {
            if (data == null || data.getData() == null) {
                return;
            }
            startCrop(null, data.getData());
        }
    }
}