Example usage for android.media ExifInterface ORIENTATION_NORMAL

List of usage examples for android.media ExifInterface ORIENTATION_NORMAL

Introduction

In this page you can find the example usage for android.media ExifInterface ORIENTATION_NORMAL.

Prototype

int ORIENTATION_NORMAL

To view the source code for android.media ExifInterface ORIENTATION_NORMAL.

Click Source Link

Usage

From source file:mobisocial.bento.todo.ui.TodoListFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (DEBUG)/* w ww . jav a 2  s .c  o m*/
        Log.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (resultCode == Activity.RESULT_OK) {
        try {
            File imageFile = null;

            if (requestCode == REQUEST_IMAGE_CAPTURE) {

                imageFile = JpgFileHelper.getTmpFile();

            } else if (requestCode == REQUEST_GALLERY) {
                Uri uri = data.getData();
                if (uri == null || uri.toString().length() == 0) {
                    return;
                }

                if (DEBUG)
                    Log.d(TAG, "data URI: " + uri.toString());

                ContentResolver cr = getActivity().getContentResolver();
                String[] columns = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
                Cursor c = cr.query(uri, columns, null, null, null);

                if (c != null && c.moveToFirst()) {
                    if (c.getString(0) != null) {
                        //regular processing for gallery files
                        imageFile = new File(c.getString(0));
                    } else {
                        final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                        imageFile = JpgFileHelper.saveTmpFile(is);
                        is.close();
                    }
                } else if (uri.toString().startsWith("content://com.android.gallery3d.provider")) {
                    // motorola xoom doesn't work for contentresolver even if the image comes from picasa.
                    // So just adds the condition of startsWith...
                    // See in detail : http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
                    uri = Uri.parse(
                            uri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
                    final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                } else {
                    // http or https
                    HttpURLConnection http = null;
                    URL url = new URL(uri.toString());
                    http = (HttpURLConnection) url.openConnection();
                    http.setRequestMethod("GET");
                    http.connect();

                    final InputStream is = http.getInputStream();
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                    if (http != null)
                        http.disconnect();
                }
            }

            if (imageFile.exists() && imageFile.length() > 0) {
                if (DEBUG)
                    Log.d(TAG, "imageFile exists=" + imageFile.exists() + " length=" + imageFile.length()
                            + " path=" + imageFile.getPath());

                float degrees = 0;
                try {
                    ExifInterface exif = new ExifInterface(imageFile.getPath());
                    switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL)) {
                    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;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Bitmap bitmap = BitmapHelper.getResizedBitmap(imageFile, BitmapHelper.MAX_IMAGE_WIDTH,
                        BitmapHelper.MAX_IMAGE_HEIGHT, degrees);

                TodoListItem item = new TodoListItem();
                item.uuid = UUID.randomUUID().toString();
                item.title = "";
                item.description = "";
                item.bDone = false;
                item.hasImage = true;
                item.creDateMillis = System.currentTimeMillis();
                item.modDateMillis = System.currentTimeMillis();
                item.creContactId = mManager.getLocalContactId();
                item.modContactId = mManager.getLocalContactId();

                StringBuilder msg = new StringBuilder(
                        getString(R.string.feed_msg_added_photo, mManager.getLocalName()));
                String plainMsg = UIUtils.getPlainString(mManager.getBentoListItem().bento.name,
                        msg.toString());

                mManager.addTodo(item, bitmap, plainMsg);

                refreshView();

                JpgFileHelper.deleteTmpFile();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:me.tipi.kiosk.ui.fragments.OCRFragment.java

@Override
public void onMetadataAvailable(Metadata metadata) {
    // This method will be called when metadata becomes available during recognition process.
    // Here, for every metadata type that is allowed through metadata settings,
    // desired actions can be performed.
    // detection metadata contains detection locations
    if (metadata instanceof DetectionMetadata) {
        // detection location is written inside DetectorResult
        DetectorResult detectorResult = ((DetectionMetadata) metadata).getDetectionResult();
        // DetectorResult can be null - this means that detection has failed
        if (detectorResult == null) {
            if (mQvManager != null) {
                // begin quadrilateral animation to its default position
                // (internally displays FAIL status)
                mQvManager.animateQuadToDefaultPosition();
            }//from www  .  j  a va2 s.c  o m
            // when points of interested have been detected (e.g. QR code), this will be returned as PointsDetectorResult
        } else if (detectorResult instanceof QuadDetectorResult) {
            // begin quadrilateral animation to detected quadrilateral
            mQvManager.animateQuadToDetectionPosition((QuadDetectorResult) detectorResult);
        }
    } else if (metadata instanceof ImageMetadata) {
        // obtain image
        // Please note that Image's internal buffers are valid only
        // until this method ends. If you want to save image for later,
        // obtained a cloned image with image.clone().

        if (guest.passportPath == null || TextUtils.isEmpty(guest.passportPath) || this.results == null) {
            Image image = ((ImageMetadata) metadata).getImage();

            if (image != null && image.getImageType() == ImageType.SUCCESSFUL_SCAN) {
                Timber.w("Metadata type is successful frame image, we go to save it");
                image.clone();
                Timber.w("Ocr meta data captured");
                final Bitmap bitmap = image.convertToBitmap();
                Timber.w("Metadata converted to bitmap");
                final Uri photoUri = ImageUtility.savePassportPicture(getActivity(), bitmap);
                Timber.w("OCR Uri got back from file helper with path: %s",
                        photoUri != null ? photoUri.getPath() : "NO OCR FILE PATH!!!!!");
                ExifInterface ei = null;
                try {
                    ei = new ExifInterface(photoUri.getPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                int orientation = ei != null
                        ? ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
                        : 0;
                Timber.w("Ocr image orientation angle is: %d", orientation);

                Bitmap rotated = null;

                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotated = rotateImage(bitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotated = rotateImage(bitmap, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotated = rotateImage(bitmap, 270);
                    break;
                case ExifInterface.ORIENTATION_UNDEFINED:
                    rotated = rotateImage(bitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                default:
                    break;
                }

                final Uri rotatedUri = ImageUtility.savePassportPicture(getActivity(), rotated);
                Timber.w("OCR ROTATED Uri got back from file helper with path: %s",
                        rotatedUri != null ? rotatedUri.getPath() : "NO OCR FILE PATH!!!!!");
                guest.passportPath = rotatedUri.getPath();
                Timber.w("Ocr path saved with path: %s", rotatedUri.getPath());
            }
        } else {
            Timber.w("We have ocr path and it's : %s", guest.passportPath);
        }
        // to convert the image to Bitmap, call image.convertToBitmap()

        // after this line, image gets disposed. If you want to save it
        // for later, you need to clone it with image.clone()
    }
}

From source file:com.klinker.android.twitter.activities.compose.Compose.java

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Log.v("talon_composing_image", "rotation: " + orientation);

    try {//  w  w  w .  j  a  v  a  2 s  .com
        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

protected void onPhotoTaken(String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }/*from  www  .  ja va  2s  .  c  o  m*/

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:com.nextgis.ngm_clink_monitoring.fragments.ObjectStatusFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    File tempPhotoFile = new File(mTempPhotoPath);

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        GISApplication app = (GISApplication) getActivity().getApplication();
        ContentResolver contentResolver = app.getContentResolver();
        String photoFileName = getPhotoFileName();

        try {/*from w ww . j  a va  2  s .c om*/
            BitmapUtil.writeLocationToExif(tempPhotoFile, app.getCurrentLocation(), 0);
        } catch (IOException e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        Uri allAttachesUri = Uri.parse("content://" + FoclSettingsConstantsUI.AUTHORITY + "/" + mObjectLayerName
                + "/" + mObjectId + "/attach");

        ContentValues values = new ContentValues();
        values.put(VectorLayer.ATTACH_DISPLAY_NAME, photoFileName);
        values.put(VectorLayer.ATTACH_MIME_TYPE, "image/jpeg");
        //values.put(VectorLayer.ATTACH_DESCRIPTION, photoFileName);

        Uri attachUri = null;
        try {
            attachUri = contentResolver.insert(allAttachesUri, values);

        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        if (null != attachUri) {

            try {
                int exifOrientation = BitmapUtil.getOrientationFromExif(tempPhotoFile);

                // resize and rotate
                Bitmap sourceBitmap = BitmapFactory.decodeFile(tempPhotoFile.getPath());
                Bitmap resizedBitmap = BitmapUtil.getResizedBitmap(sourceBitmap,
                        FoclConstants.PHOTO_MAX_SIZE_PX, FoclConstants.PHOTO_MAX_SIZE_PX);
                Bitmap rotatedBitmap = BitmapUtil.rotateBitmap(resizedBitmap, exifOrientation);

                // jpeg compress
                File tempAttachFile = File.createTempFile("attach", null, app.getCacheDir());
                OutputStream tempOutStream = new FileOutputStream(tempAttachFile);
                rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, FoclConstants.PHOTO_JPEG_COMPRESS_QUALITY,
                        tempOutStream);
                tempOutStream.close();

                int newHeight = rotatedBitmap.getHeight();
                int newWidth = rotatedBitmap.getWidth();

                rotatedBitmap.recycle();

                // write EXIF to new file
                BitmapUtil.copyExifData(tempPhotoFile, tempAttachFile);

                ExifInterface attachExif = new ExifInterface(tempAttachFile.getCanonicalPath());

                attachExif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + ExifInterface.ORIENTATION_NORMAL);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, "" + newHeight);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "" + newWidth);

                attachExif.saveAttributes();

                // attach data from tempAttachFile
                OutputStream attachOutStream = contentResolver.openOutputStream(attachUri);
                FoclFileUtil.copy(new FileInputStream(tempAttachFile), attachOutStream);
                attachOutStream.close();

                tempAttachFile.delete();

            } catch (IOException e) {
                Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            Log.d(TAG, attachUri.toString());

        } else {
            Log.d(TAG, "insert attach failed");
        }

        try {
            if (app.isOriginalPhotoSaving()) {
                File origPhotoFile = new File(getDailyPhotoFolder(), photoFileName);

                if (!com.nextgis.maplib.util.FileUtil.move(tempPhotoFile, origPhotoFile)) {
                    Toast.makeText(getActivity(), "Save original photo failed", Toast.LENGTH_LONG).show();
                }

            } else {
                tempPhotoFile.delete();
            }

            setPhotoGalleryAdapter();
            setPhotoGalleryVisibility(true);

        } catch (IOException e) {
            Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    }

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_CANCELED) {
        tempPhotoFile.delete();
    }
}

From source file:mobisocial.bento.ebento.ui.EditFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (DEBUG)//from  w ww  . j a  v a 2 s .c o m
        Log.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (resultCode == Activity.RESULT_OK) {
        try {
            File imageFile = null;

            if (requestCode == REQUEST_IMAGE_CAPTURE) {

                imageFile = JpgFileHelper.getTmpFile();

            } else if (requestCode == REQUEST_GALLERY) {
                Uri uri = data.getData();
                if (uri == null || uri.toString().length() == 0) {
                    return;
                }
                if (DEBUG)
                    Log.d(TAG, "data URI: " + uri.toString());

                ContentResolver cr = getActivity().getContentResolver();
                String[] columns = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
                Cursor c = cr.query(uri, columns, null, null, null);

                if (c != null && c.moveToFirst()) {
                    if (c.getString(0) != null) {
                        //regular processing for gallery files
                        imageFile = new File(c.getString(0));
                    } else {
                        final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                        imageFile = JpgFileHelper.saveTmpFile(is);
                        is.close();
                    }
                } else if (uri.toString().startsWith("content://com.android.gallery3d.provider")) {
                    // motorola xoom doesn't work for contentresolver even if the image comes from picasa.
                    // So just adds the condition of startsWith...
                    // See in detail : http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
                    uri = Uri.parse(
                            uri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
                    final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                } else {
                    // http or https
                    HttpURLConnection http = null;
                    URL url = new URL(uri.toString());
                    http = (HttpURLConnection) url.openConnection();
                    http.setRequestMethod("GET");
                    http.connect();

                    final InputStream is = http.getInputStream();
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                    if (http != null)
                        http.disconnect();
                }
            }

            if (imageFile.exists() && imageFile.length() > 0) {
                if (DEBUG)
                    Log.d(TAG, "imageFile exists=" + imageFile.exists() + " length=" + imageFile.length()
                            + " path=" + imageFile.getPath());

                float degrees = 0;
                try {
                    ExifInterface exif = new ExifInterface(imageFile.getPath());
                    switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL)) {
                    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;
                    }
                    Log.d(TAG, exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                sImageOrg = BitmapHelper.getResizedBitmap(imageFile, BitmapHelper.MAX_IMAGE_WIDTH,
                        BitmapHelper.MAX_IMAGE_HEIGHT, degrees);
                mbImageChanged = true;

                // ImageView
                mImageView.setImageBitmap(
                        BitmapHelper.getResizedBitmap(sImageOrg, mTargetWidth, mTargetHeight, 0));
                mImageView.setVisibility(View.VISIBLE);
                mRootView.invalidate();

                imageFile.delete();
                JpgFileHelper.deleteTmpFile();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.waz.zclient.pages.main.drawing.DrawingFragment.java

private ImageAsset getFinalSketchImage() {
    Bitmap finalBitmap = getBitmapDrawing();
    try {//from  w ww.  ja  v a2  s  .co m
        Rect bitmapTrim = drawingCanvasView.getImageTrimValues();
        MemoryImageCache.reserveImageMemory(bitmapTrim.width(), bitmapTrim.height());
        finalBitmap = Bitmap.createBitmap(finalBitmap, bitmapTrim.left, bitmapTrim.top, bitmapTrim.width(),
                bitmapTrim.height());
    } catch (Throwable t) {
        // ignore
    }
    return ImageAssetFactory.getImageAsset(finalBitmap, ExifInterface.ORIENTATION_NORMAL);
}

From source file:com.snappy.CameraCropActivity.java

protected void onPhotoTaken(final String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }/*ww w .ja  v a 2 s.  c  o m*/

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (!path.equals(mFilePath)) {
                    copy(new File(path), new File(mFilePath));
                }

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:com.bai.android.ui.OtherActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CHANGE_RINGTONE_REQUEST_CODE && resultCode == RESULT_OK) {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        if (uri != null) {
            String ringTonePath = uri.toString();

            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(getResources().getString(R.string.default_sound), ringTonePath);
            editor.commit();//from ww w . j a va  2  s .c  om
            pref = null;

        }
    } else {

        int rotation = ExifInterface.ORIENTATION_NORMAL;

        if (requestCode == GALLERY_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK
                && data.getData() != null) {
            Uri imageUri = data.getData();
            setAvatarFromInputStreamUri(imageUri);
            rotation = getOrientationFromMediaStore(getApplicationContext(), imageUri);
            rotateImageIfNecessary(rotation);
            if (avatarPicture != null) {
                avatar.setImageBitmap(avatarPicture);
            }
        } else if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            String imagePath = getSharedPreferences(SHR_PRF_APP_KEY, MODE_PRIVATE).getString(SHR_PRF_IMG_URI,
                    null);

            setAvatarFromFilePath(imagePath);
            if (avatarPicture == null) {
                Toast.makeText(getApplicationContext(), "Image cannot be decoded", Toast.LENGTH_LONG).show();
                return;
            }

            rotation = getOrientationFromFile(imagePath);
            //         }
            rotateImageIfNecessary(rotation);
            if (avatarPicture != null) {
                avatar.setImageBitmap(avatarPicture);
            }

        }

    }
}

From source file:com.bai.android.ui.OtherActivity.java

private static int getOrientationFromFile(String imagePath) {
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    int rotation;
    // get orientation of file
    try {//from ww w.  j a  v a 2 s  .  c  o  m
        ExifInterface exif = new ExifInterface(imagePath);
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // set the rotation from the file orientation
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotation = 90;
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotation = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotation = 180;
        break;
    default:
        rotation = ExifInterface.ORIENTATION_NORMAL;
        break;
    }
    return rotation;
}