Skip to content

Solved Issues: #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
135 changes: 86 additions & 49 deletions app/src/main/java/droidmentor/imageattachment/Imageutils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -45,7 +49,6 @@ public class Imageutils

private ImageAttachmentListener imageAttachment_callBack;

private String selected_path="";
private Uri imageUri;
private File path = null;

Expand All @@ -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;
Expand All @@ -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;
Copy link
Author

@1raghavmahajan 1raghavmahajan Jun 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line #87.

if (isFragment) {
this.isFragment = true;
current_fragment = fragment;
}
imageAttachment_callBack = listener;
}

/**
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -386,7 +404,7 @@ public void launchGallery(int from)
}
else
{
galley_call();
gallery_call();
}
}

Expand Down Expand Up @@ -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();
}


Expand All @@ -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)) {
Expand All @@ -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)
Expand Down Expand Up @@ -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/*");
Expand Down Expand Up @@ -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();
Expand All @@ -605,57 +624,72 @@ 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);
}
catch(Exception e)
{
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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}


Expand Down