Skip to content

Commit

Permalink
Merge pull request #155 from yogeshpaliyal/android12-compatible
Browse files Browse the repository at this point in the history
Pick from gallery compatible to android 12
  • Loading branch information
yogeshpaliyal committed Mar 25, 2022
2 parents afd51b4 + 1d509f8 commit 1980679
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
4 changes: 1 addition & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ buildscript {

android {
compileSdkVersion 31
buildToolsVersion '28.0.2'
defaultConfig {
applicationId 'com.vansuita.pickimage.sample'
minSdkVersion 14
Expand All @@ -23,8 +22,7 @@ android {
dependencies {
implementation project(':library')

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.jrummyapps:colorpicker:2.0.4'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import com.vansuita.pickimage.enums.EPickType;
import com.vansuita.pickimage.sample.R;

import org.jetbrains.annotations.Nullable;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.vansuita.pickimage.bean.PickResult;
import com.vansuita.pickimage.bundle.PickSetup;
import com.vansuita.pickimage.dialog.PickImageDialog;
import com.vansuita.pickimage.listeners.IPickResult;

import org.jetbrains.annotations.Nullable;

public class SampleActivity extends BaseSampleActivity implements IPickResult /*, IPickClick */ {

Expand Down
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ buildscript {

dependencies {

classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // Add this line
classpath 'com.android.tools.build:gradle:7.1.2'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jul 22 17:41:01 IST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
16 changes: 8 additions & 8 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'


group = 'com.github.jrvansuita'

android {
compileSdkVersion 29
buildToolsVersion "28.0.3"
compileSdkVersion 31

defaultConfig {
minSdkVersion 9
targetSdkVersion 29
versionCode 1
versionName "1.0"
targetSdkVersion 31
}

sourceSets {
Expand All @@ -21,8 +17,12 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.annotation:annotation:1.0.0'
implementation 'androidx.annotation:annotation:1.3.0'
implementation "androidx.core:core-ktx:1.7.0"
}
repositories {
mavenCentral()
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.webkit.MimeTypeMap;

import com.vansuita.pickimage.bundle.PickSetup;
import com.vansuita.pickimage.enums.EPickType;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import static android.graphics.BitmapFactory.decodeStream;

Expand Down Expand Up @@ -160,8 +172,68 @@ public String getUriPath() {
if (provider.equals(EPickType.CAMERA)) {
return uri.getPath();
} else {
return getGalleryPath();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return getGalleryNewPath(uri);
} else {
return getGalleryPath();
}

}
}

private String getGalleryNewPath(Uri uri){
File folder = new File(context.getFilesDir()+"/Pictures");
if(!folder.exists()){
folder.mkdir();
}

File galleryFile = new File(folder.getAbsolutePath(), getGalleryFileName(uri));
try {
FileOutputStream fos = new FileOutputStream(galleryFile);
InputStream fis = context.getContentResolver().openInputStream(uri);
copy(fis, fos);
fos.close();
fis.close();
} catch (Exception e){
e.printStackTrace();
}
return galleryFile.getPath();

}

void copy(InputStream source, OutputStream target) throws IOException {
byte[] buf = new byte[8192];
int length;
while ((length = source.read(buf)) > 0) {
target.write(buf, 0, length);
}
}

private String getGalleryFileName(Uri uri) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, null, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return "FILE_"+getTimestampString()+"."+getExtension(uri);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

private String getExtension(Uri uri) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
return (extension != null) ? extension : "";
}

private String getTimestampString() {
Calendar date = Calendar.getInstance();
return new SimpleDateFormat("yyyy MM dd hh mm ss", Locale.US).format(date.getTime()).replace(" ", "");
}

private String getGalleryPath() {
Expand Down

0 comments on commit 1980679

Please sign in to comment.