From 25184b57584a2492858c22b334ec3f72f489e3b1 Mon Sep 17 00:00:00 2001 From: Raghav Date: Wed, 7 Jun 2017 03:16:16 +0530 Subject: [PATCH] Solved Issues: - The API didn't actually take the picture after asking for permission unless clicked again. - Added support for custom file names while implementing the listener. - Added support for inline listeners as opposed to Activity/Fragment implementation. - Added support for higher API Fragment. --- .idea/modules.xml | 2 + app/build.gradle | 12 +- .../imageattachment/AttachmentFragment.java | 2 +- .../imageattachment/Imageutils.java | 135 +++++++++++------- 4 files changed, 95 insertions(+), 56 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 856bb9a..ea7566f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,8 @@ + + diff --git a/app/build.gradle b/app/build.gradle index c4dcb07..3d6defa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion '25.0.0' + compileSdkVersion 25 + buildToolsVersion '25.0.3' defaultConfig { applicationId "droidmentor.imageattachment" minSdkVersion 15 - targetSdkVersion 23 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -21,8 +21,8 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1' - compile 'com.android.support:support-v4:23.4.0' + compile 'com.android.support:appcompat-v7:25.3.1' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.android.support:support-v4:25.3.1' testCompile 'junit:junit:4.12' } diff --git a/app/src/main/java/droidmentor/imageattachment/AttachmentFragment.java b/app/src/main/java/droidmentor/imageattachment/AttachmentFragment.java index 38180fb..39e6bf5 100644 --- a/app/src/main/java/droidmentor/imageattachment/AttachmentFragment.java +++ b/app/src/main/java/droidmentor/imageattachment/AttachmentFragment.java @@ -1,13 +1,13 @@ package droidmentor.imageattachment; +import android.app.Fragment; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; diff --git a/app/src/main/java/droidmentor/imageattachment/Imageutils.java b/app/src/main/java/droidmentor/imageattachment/Imageutils.java index 9acc9ec..eecdbed 100755 --- a/app/src/main/java/droidmentor/imageattachment/Imageutils.java +++ b/app/src/main/java/droidmentor/imageattachment/Imageutils.java @@ -3,6 +3,7 @@ import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.Fragment; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; @@ -19,8 +20,8 @@ import android.os.Build; import android.provider.MediaStore; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; -import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.util.Base64; @@ -31,9 +32,12 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.text.DateFormat; +import java.util.Date; import static android.content.ContentValues.TAG; +@SuppressWarnings({"WeakerAccess", "JavaDoc", "unused"}) @SuppressLint("SdCardPath") public class Imageutils { @@ -45,7 +49,6 @@ public class Imageutils private ImageAttachmentListener imageAttachment_callBack; - private String selected_path=""; private Uri imageUri; private File path = null; @@ -59,6 +62,12 @@ public Imageutils(Activity act) { imageAttachment_callBack=(ImageAttachmentListener)context; } + public Imageutils(Activity act, ImageAttachmentListener listener) { + this.context = act; + this.current_activity = act; + imageAttachment_callBack = listener; + } + public Imageutils(Activity act,Fragment fragment,boolean isFragment) { this.context=act; @@ -69,7 +78,18 @@ public Imageutils(Activity act,Fragment fragment,boolean isFragment) { this.isFragment=true; current_fragment=fragment; } + } + + public Imageutils(Activity act, Fragment fragment, boolean isFragment, ImageAttachmentListener listener) { + this.context = act; + this.current_activity = act; + imageAttachment_callBack = (ImageAttachmentListener) fragment; + if (isFragment) { + this.isFragment = true; + current_fragment = fragment; + } + imageAttachment_callBack = listener; } /** @@ -165,14 +185,10 @@ public String BitMapToString(Bitmap bitmap){ */ public boolean isDeviceSupportCamera() { - if (this.context.getPackageManager().hasSystemFeature( - PackageManager.FEATURE_CAMERA)) { - // this device has a camera - return true; - } else { - // no camera on this device - return false; - } + // this device has a camera +// no camera on this device + return this.context.getPackageManager().hasSystemFeature( + PackageManager.FEATURE_CAMERA); } @@ -310,7 +326,9 @@ private String getRealPathFromURI(String contentURI) { } else { cursor.moveToFirst(); int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); - return cursor.getString(index); + String res = cursor.getString(index); + cursor.close(); + return res; } } @@ -386,7 +404,7 @@ public void launchGallery(int from) } else { - galley_call(); + gallery_call(); } } @@ -463,19 +481,22 @@ public void onClick(DialogInterface dialog, int which) { code); } }); - return; + } else { + ActivityCompat.requestPermissions(current_activity, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + code); } + if (code == 1) + camera_call(); + else if (code == 2) + gallery_call(); - ActivityCompat.requestPermissions(current_activity, - new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, - code); - return; } if(code==1) camera_call(); else if(code==2) - galley_call(); + gallery_call(); } @@ -492,7 +513,6 @@ public void permission_check_fragment(final int code) Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) - { if (!ActivityCompat.shouldShowRequestPermissionRationale(current_activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { @@ -501,30 +521,29 @@ public void permission_check_fragment(final int code) new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - - current_fragment.requestPermissions( + ActivityCompat.requestPermissions(current_activity, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, code); } }); return; + } else { + ActivityCompat.requestPermissions(current_activity, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + code); } - - current_fragment.requestPermissions( - new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, - code); - return; + if (code == 1) + camera_call(); + else if (code == 2) + gallery_call(); } - if(code==1) camera_call(); else if(code==2) - galley_call(); + gallery_call(); } - - private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(current_activity) .setMessage(message) @@ -558,9 +577,9 @@ public void camera_call() * */ - public void galley_call() + public void gallery_call() { - Log.d(TAG, "galley_call: "); + Log.d(TAG, "gallery_call: "); Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent2.setType("image/*"); @@ -595,7 +614,7 @@ public void request_permission_result(int requestCode, @NonNull String[] permiss case 2: // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - galley_call(); + gallery_call(); } else { Toast.makeText(current_activity, "Permission denied", Toast.LENGTH_LONG).show(); @@ -605,34 +624,52 @@ public void request_permission_result(int requestCode, @NonNull String[] permiss } + /** + * Intent ActivityResult + * @param requestCode + * @param resultCode + * @param data + */ + public void onActivityResult(int requestCode, int resultCode, Intent data) { + onActivityResult(requestCode, resultCode, data, null); + } + /** * Intent ActivityResult * * @param requestCode * @param resultCode * @param data + * @param fileName optional */ - public void onActivityResult(int requestCode, int resultCode, Intent data) + public void onActivityResult(int requestCode, int resultCode, Intent data, @Nullable String fileName) { String file_name; + if (fileName != null) { + file_name = fileName; + } else { + String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); + currentDateTimeString = currentDateTimeString.replaceAll(" ", "_"); + currentDateTimeString = currentDateTimeString.replaceAll(",", ""); + currentDateTimeString = currentDateTimeString.replaceAll(":", "."); + currentDateTimeString = "IMG_" + currentDateTimeString + ".jpg"; + file_name = currentDateTimeString; + } Bitmap bitmap; switch (requestCode) { case 0: - if(resultCode==current_activity.RESULT_OK) + String selected_path = ""; + if (resultCode == Activity.RESULT_OK) { Log.i("Camera Selected","Photo"); try { - selected_path=null; - selected_path=getPath(imageUri); - // Log.i("selected","path"+selected_path); - file_name =selected_path.substring(selected_path.lastIndexOf("/")+1); - // Log.i("file","name"+file_name); + Log.i("YOYO", "name" + file_name); bitmap =compressImage(imageUri.toString(),816,612); imageAttachment_callBack.image_attachment(from, file_name, bitmap,imageUri); } @@ -640,22 +677,19 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { e.printStackTrace(); } - - - } break; case 1: - if(resultCode==current_activity.RESULT_OK) + if (resultCode == Activity.RESULT_OK) { Log.i("Gallery","Photo"); Uri selectedImage=data.getData(); try { - selected_path=null; - selected_path=getPath(selectedImage); - file_name =selected_path.substring(selected_path.lastIndexOf("/")+1); + selected_path = null; + selected_path = getPath(selectedImage); + file_name = selected_path.substring(selected_path.lastIndexOf("/") + 1); bitmap =compressImage(selectedImage.toString(),816,612); imageAttachment_callBack.image_attachment(from, file_name, bitmap,selectedImage); } @@ -795,7 +829,10 @@ public void createImage(Bitmap bitmap, String file_name, String filepath, boolea if(!path.exists()) { - path.mkdirs(); + if (!path.mkdirs()) { + //could not make dir + return; + } } File file = new File(path,file_name); @@ -840,7 +877,7 @@ public void store_image(File file, Bitmap bmp) // Image Attachment Callback public interface ImageAttachmentListener { - public void image_attachment(int from, String filename, Bitmap file, Uri uri); + void image_attachment(int from, String filename, Bitmap file, Uri uri); }