├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── inthecheesefactory │ │ └── lab │ │ └── intent_fileprovider │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── inthecheesefactory │ │ │ └── lab │ │ │ └── intent_fileprovider │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── inthecheesefactory │ └── lab │ └── intent_fileprovider │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lab-Intent-FileProvider -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'android-apt' 3 | 4 | android { 5 | compileSdkVersion 24 6 | buildToolsVersion "24.0.0" 7 | defaultConfig { 8 | applicationId "com.inthecheesefactory.lab.intent_fileprovider" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 26 | exclude group: 'com.android.support', module: 'support-annotations' 27 | }) 28 | compile 'com.android.support:appcompat-v7:24.1.1' 29 | compile 'com.github.hotchemi:permissionsdispatcher:2.1.3' 30 | apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.3' 31 | testCompile 'junit:junit:4.12' 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Neois\Installer\AndroidSDK\android-sdk-windows/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/inthecheesefactory/lab/intent_fileprovider/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.intent_fileprovider; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.inthecheesefactory.lab.intent_fileprovider", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/inthecheesefactory/lab/intent_fileprovider/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.intent_fileprovider; 2 | 3 | import android.Manifest; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.graphics.Bitmap; 7 | import android.graphics.BitmapFactory; 8 | import android.media.MediaScannerConnection; 9 | import android.net.Uri; 10 | import android.os.Environment; 11 | import android.provider.MediaStore; 12 | import android.support.v4.content.FileProvider; 13 | import android.support.v7.app.AlertDialog; 14 | import android.support.v7.app.AppCompatActivity; 15 | import android.os.Bundle; 16 | import android.view.View; 17 | import android.widget.Button; 18 | import android.widget.ImageView; 19 | 20 | import java.io.File; 21 | import java.io.FileInputStream; 22 | import java.io.FileNotFoundException; 23 | import java.io.IOException; 24 | import java.io.InputStream; 25 | import java.text.SimpleDateFormat; 26 | import java.util.Date; 27 | 28 | import permissions.dispatcher.NeedsPermission; 29 | import permissions.dispatcher.OnShowRationale; 30 | import permissions.dispatcher.PermissionRequest; 31 | import permissions.dispatcher.RuntimePermissions; 32 | 33 | @RuntimePermissions 34 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 35 | 36 | private static final int REQUEST_TAKE_PHOTO = 1; 37 | 38 | Button btnTakePhoto; 39 | ImageView ivPreview; 40 | 41 | String mCurrentPhotoPath; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_main); 47 | 48 | initInstances(); 49 | } 50 | 51 | private void initInstances() { 52 | btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto); 53 | ivPreview = (ImageView) findViewById(R.id.ivPreview); 54 | 55 | btnTakePhoto.setOnClickListener(this); 56 | } 57 | 58 | ///////////////////// 59 | // OnClickListener // 60 | ///////////////////// 61 | 62 | @Override 63 | public void onClick(View view) { 64 | if (view == btnTakePhoto) { 65 | MainActivityPermissionsDispatcher.startCameraWithCheck(this); 66 | } 67 | } 68 | 69 | //////////// 70 | // Camera // 71 | //////////// 72 | 73 | @NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) 74 | void startCamera() { 75 | try { 76 | dispatchTakePictureIntent(); 77 | } catch (IOException e) { 78 | } 79 | } 80 | 81 | @OnShowRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE) 82 | void showRationaleForCamera(final PermissionRequest request) { 83 | new AlertDialog.Builder(this) 84 | .setMessage("Access to External Storage is required") 85 | .setPositiveButton("Allow", new DialogInterface.OnClickListener() { 86 | @Override 87 | public void onClick(DialogInterface dialogInterface, int i) { 88 | request.proceed(); 89 | } 90 | }) 91 | .setNegativeButton("Deny", new DialogInterface.OnClickListener() { 92 | @Override 93 | public void onClick(DialogInterface dialogInterface, int i) { 94 | request.cancel(); 95 | } 96 | }) 97 | .show(); 98 | } 99 | 100 | @Override 101 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 102 | super.onActivityResult(requestCode, resultCode, data); 103 | if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { 104 | // Show the thumbnail on ImageView 105 | Uri imageUri = Uri.parse(mCurrentPhotoPath); 106 | File file = new File(imageUri.getPath()); 107 | try { 108 | InputStream ims = new FileInputStream(file); 109 | ivPreview.setImageBitmap(BitmapFactory.decodeStream(ims)); 110 | } catch (FileNotFoundException e) { 111 | return; 112 | } 113 | 114 | // ScanFile so it will be appeared on Gallery 115 | MediaScannerConnection.scanFile(MainActivity.this, 116 | new String[]{imageUri.getPath()}, null, 117 | new MediaScannerConnection.OnScanCompletedListener() { 118 | public void onScanCompleted(String path, Uri uri) { 119 | } 120 | }); 121 | } 122 | } 123 | 124 | @Override 125 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 126 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 127 | MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); 128 | } 129 | 130 | private File createImageFile() throws IOException { 131 | // Create an image file name 132 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 133 | String imageFileName = "JPEG_" + timeStamp + "_"; 134 | File storageDir = new File(Environment.getExternalStoragePublicDirectory( 135 | Environment.DIRECTORY_DCIM), "Camera"); 136 | File image = File.createTempFile( 137 | imageFileName, /* prefix */ 138 | ".jpg", /* suffix */ 139 | storageDir /* directory */ 140 | ); 141 | 142 | // Save a file: path for use with ACTION_VIEW intents 143 | mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 144 | return image; 145 | } 146 | 147 | private void dispatchTakePictureIntent() throws IOException { 148 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 149 | // Ensure that there's a camera activity to handle the intent 150 | if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 151 | // Create the File where the photo should go 152 | File photoFile = null; 153 | try { 154 | photoFile = createImageFile(); 155 | } catch (IOException ex) { 156 | // Error occurred while creating the File 157 | return; 158 | } 159 | // Continue only if the File was successfully created 160 | if (photoFile != null) { 161 | Uri photoURI = Uri.fromFile(createImageFile()); 162 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 163 | startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 164 | } 165 | } 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 |