Skip to content

Commit

Permalink
stable, without compressor
Browse files Browse the repository at this point in the history
  • Loading branch information
hangga committed May 31, 2022
1 parent 7359a4b commit b94f9ea
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 212 deletions.
5 changes: 3 additions & 2 deletions picker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

group = 'io.kredibel' // <-- group name
archivesBaseName = "picker" // <-- artifact name
version = "0.0.3"
version = "0.0.4-beta1"

publishing {

Expand Down Expand Up @@ -60,9 +60,10 @@ android {

dependencies {

//implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.exifinterface:exifinterface:1.3.2'
implementation 'androidx.exifinterface:exifinterface:1.3.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
195 changes: 0 additions & 195 deletions picker/src/main/java/io/kredibel/picker/Compressor.java

This file was deleted.

86 changes: 71 additions & 15 deletions picker/src/main/java/io/kredibel/picker/PickerObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
Expand All @@ -22,30 +24,22 @@
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.Random;

import static android.app.Activity.RESULT_OK;

public class PickerObserver implements DefaultLifecycleObserver {
private final ActivityResultRegistry registry;
private final Activity activity;
private final Compressor compressor;
//private final Compressor compressor;
private PickerListener pickerListener;
ActivityResultCallback<Uri> galleryResultUri = new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
if (uri != null) {
try {
File file = compressor.compressUritoFile(uri);
pickerListener.onPicked(uri, file, uriToBitmap(uri));
} catch (IOException e) {
throw new RuntimeException(e);
}

File file = new File(uri.getPath());
pickerListener.onPicked(uri, file, uriToBitmap(uri));
}
}
};
Expand All @@ -54,7 +48,9 @@ public void onActivityResult(Uri uri) {
public void onActivityResult(ActivityResult result) {
int resultCode = result.getResultCode();
Intent data = result.getData();
if (resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();

if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
//File file = compressor.compressToFile()
Expand Down Expand Up @@ -84,14 +80,14 @@ public void onActivityResult(ActivityResult result) {
private ActivityResultLauncher<Intent> cameraLauncher;

public PickerObserver(@NonNull AppCompatActivity activity) {
this.compressor = new Compressor(activity);
//this.compressor = new Compressor(activity);
this.activity = activity;
this.registry = activity.getActivityResultRegistry();
}

public PickerObserver(@NonNull Fragment fr) {
this.activity = fr.requireActivity();
this.compressor = new Compressor(this.activity);
//this.compressor = new Compressor(this.activity);
this.registry = fr.requireActivity().getActivityResultRegistry();
}

Expand Down Expand Up @@ -126,4 +122,64 @@ public void pickCamera(PickerListener pickerListener) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(intent);
}

private String getRealPathFromUri(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = activity.getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

public void importFile(Uri uri) {
String fileName = getFileName(uri);

// The temp file could be whatever you want
//File fileCopy = copyToTempFile(uri, File tempFile)

// Done!
}

private String getFileName(Uri uri) throws IllegalArgumentException {
// Obtain a cursor with information regarding this uri
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);

if (cursor.getCount() <= 0) {
cursor.close();
throw new IllegalArgumentException("Can't obtain file name, cursor is empty");
}

cursor.moveToFirst();

String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));

cursor.close();

return fileName;
}


private File copyToTempFile(Uri uri, File tempFile) throws IOException {
// Obtain an input stream from the uri
InputStream inputStream = activity.getContentResolver().openInputStream(uri);

if (inputStream == null) {
throw new IOException("Unable to obtain input stream from URI");
}

// Copy the stream to the temp file
//FileUtils.copyInputStreamToFile(inputStream, tempFile);

return tempFile;
}


}

0 comments on commit b94f9ea

Please sign in to comment.