package androidpath.ll.leshare.Helper;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import androidpath.ll.leshare.R;
import androidpath.ll.leshare.View.MainActivity;

/**
 * Created by Le on 2015/5/9.
 */
public class MediaHelper {
    public static final String TAG = MediaHelper.class.getSimpleName();
    // media type code
    public static final int MEDIA_TYPE_IMAGE = 4;
    public static final int MEDIA_TYPE_VIDEO = 5;

    /**
     * @param context        to access res/strings.xml
     * @param mediaTypeImage
     * @return Uri
     * @see <a href="http://developer.android.com/guide/topics/media/camera.html">Camera guide</a>
     */

    public static Uri getOutputMediaFileUri(Context context, int mediaTypeImage) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.
        if (isExternalStorageAvailable()) {
            //1. get the external storage directory
            String appname = context.getString(R.string.app_name);
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), appname);
            //2. Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(appname, "failed to create directory");
                    return null;
                }
            }
            //3. create a file name
            //4. create file
            File mediaFile;
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
            if (mediaTypeImage == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                        "IMG_" + timeStamp + ".jpg");
            } else if (mediaTypeImage == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                        "VID_" + timeStamp + ".mp4");
            } else {
                return null;
            }
            Log.d(TAG, "File : " + Uri.fromFile(mediaFile));
            //5. return file's uri
            return Uri.fromFile(mediaFile);
        } else {
            return null;
        }
    }

    private static boolean isExternalStorageAvailable() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
}

@@ -84,46 +84,182 @@
 oid.
-net.Uri;%0Aimport android.os.Environment
+database.Cursor;%0Aimport android.net.Uri;%0Aimport android.os.Build;%0Aimport android.os.Environment;%0Aimport android.provider.DocumentsContract;%0Aimport android.provider.MediaStore
 ;%0Aim
@@ -423,57 +423,8 @@
 e.R;
-%0Aimport androidpath.ll.leshare.View.MainActivity;
 %0A%0A/*
@@ -2437,24 +2437,25 @@
 ediaFile));%0A
+%0A
             
@@ -2839,10 +2839,1884 @@
 %7D%0A    %7D%0A
+%0A    public static int getExifOrientation(Uri uri, Context context) %7B%0A        Cursor cursor = null;%0A        try %7B%0A            String%5B%5D CONTENT_ORIENTATION = %7BMediaStore.Images.Media.ORIENTATION%7D;%0A%0A            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) %7B%0A                String id = DocumentsContract.getDocumentId(uri);%0A                id = id.split(%22:%22)%5B1%5D;%0A                cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, CONTENT_ORIENTATION, MediaStore.Images.Media._ID + %22 = ?%22, new String%5B%5D%7Bid%7D, null);%0A            %7D else %7B%0A%0A                cursor = context.getContentResolver().query(uri, CONTENT_ORIENTATION, null, null, null);%0A            %7D%0A            if (cursor == null %7C%7C !cursor.moveToFirst()) %7B%0A                return 0;%0A            %7D%0A            int orientation = cursor.getInt(cursor.getColumnIndex(CONTENT_ORIENTATION%5B0%5D));%0A            return orientation;%0A        %7D catch (RuntimeException ignored) %7B%0A            // If the orientation column doesn't exist, assume no rotation.%0A        %7D%0A        return -1;%0A    %7D%0A%0A    public static String getImagePath(Context context, Uri uri) %7B%0A        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);%0A        cursor.moveToFirst();%0A        String document_id = cursor.getString(0);%0A        document_id = document_id.substring(document_id.lastIndexOf(%22:%22) + 1);%0A        cursor.close();%0A%0A        cursor = context.getContentResolver().query(%0A                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,%0A                null, MediaStore.Images.Media._ID + %22 = ? %22, new String%5B%5D%7Bdocument_id%7D, null);%0A        cursor.moveToFirst();%0A        String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));%0A        cursor.close();%0A        return path;%0A    %7D%0A%0A
 %7D%0A

add getExifOrientation(Uri uri, Context context) to access orientation from Uri. notice this method treat sdk version above Kitkat differently.