package com.mlsdev.rximagepicker;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

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

public class HiddenActivity extends Activity {
    public static String IMAGE_SOURCE = "image_source";

    private static String TAG = "RxImagePicker";

    private static final int SELECT_PHOTO = 100;
    private static final int TAKE_PHOTO = 101;

    private Uri cameraPictureUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            handleIntent(getIntent());
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            handleIntent(getIntent());
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case SELECT_PHOTO:
                    RxImagePicker.with(this).onImagePicked(data.getData());
                    break;
                case TAKE_PHOTO:
                    RxImagePicker.with(this).onImagePicked(cameraPictureUrl);
                    break;
            }
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxImagePicker.with(this).onDestroy();
    }

    private void handleIntent(Intent intent) {
        Sources sourceType = Sources.values()[intent.getIntExtra(IMAGE_SOURCE, 0)];
        int chooseCode = 0;
        Intent pictureChooseIntent = null;
        switch (sourceType) {
            case CAMERA:
                cameraPictureUrl = Uri.fromFile(createImageFile());
                pictureChooseIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                pictureChooseIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPictureUrl);
                chooseCode = TAKE_PHOTO;
                break;
            case GALLERY:
                if (!checkPermission()) {
                    return;
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    pictureChooseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    pictureChooseIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                    pictureChooseIntent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                } else {
                    pictureChooseIntent = new Intent(Intent.ACTION_GET_CONTENT);
                }
                pictureChooseIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                pictureChooseIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                pictureChooseIntent.setType("image/*");
                chooseCode = SELECT_PHOTO;
                break;
        }
        startActivityForResult(pictureChooseIntent, chooseCode);
    }

    private boolean checkPermission() {
        if (ContextCompat.checkSelfPermission(HiddenActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(HiddenActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    0);
            return false;
        } else {
            return true;
        }
    }

    private File createImageFile() {
        File imageTempFile = null;
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File storageDir = getExternalFilesDir(null);
        try {
            imageTempFile = File.createTempFile(
                    timeStamp,
                    ".jpg",
                    storageDir
            );
        } catch (IOException ex) {
            Log.e(TAG, ex.toString());
        }
        return imageTempFile;
    }

    private String getImagePath(Uri uri) {
        String result = null;
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
            cursor.close();
        }
        return result;
    }

}


@@ -82,16 +82,94 @@
 tivity;%0A
+import android.content.ContentResolver;%0Aimport android.content.ContentValues;%0A
 import a
@@ -475,83 +475,8 @@
 at;%0A
-import android.util.Log;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0A
 impo
@@ -2358,16 +2358,104 @@
 CAMERA:%0A
+                if (!checkPermission()) %7B%0A                    return;%0A                %7D%0A
         
@@ -2485,21 +2485,8 @@
 l = 
-Uri.fromFile(
 crea
@@ -2492,22 +2492,20 @@
 ateImage
-File()
+Uri(
 );%0A     
@@ -4153,20 +4153,19 @@
 private 
-File
+Uri
  createI
@@ -4168,20 +4168,19 @@
 ateImage
-File
+Uri
 () %7B%0A   
@@ -4188,33 +4188,110 @@
     
-File imageTempFile = null
+ContentResolver contentResolver = getContentResolver();%0A        ContentValues cv = new ContentValues()
 ;%0A  
@@ -4408,333 +4408,145 @@
     
-File storageDir = getExternalFilesDir(null);%0A        try %7B%0A            imageTempFile = File.createTempFile(%0A                    timeStamp,%0A                    %22.jpg%22,%0A                    storageDir%0A            );%0A        %7D catch (IOException ex) %7B%0A            Log.e(TAG, ex.toString());%0A        %7D%0A        return imageTempFile
+cv.put(MediaStore.Images.Media.TITLE, timeStamp);%0A        return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv)
 ;%0A  
@@ -5072,8 +5072,5 @@
   %7D%0A
-%0A%7D%0A%0A
+%7D

Use content resolver and MediaStore instead of temp files