Skip to content

Commit

Permalink
Merge pull request #1 from kredibel-id/develop
Browse files Browse the repository at this point in the history
[Update] stable -> camera, gallery
  • Loading branch information
hangga authored May 31, 2022
2 parents b52e617 + ee49f55 commit b211fbe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 98 deletions.
33 changes: 27 additions & 6 deletions app/src/main/java/id/co/kredibel/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package id.co.kredibel;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import io.kredibel.picker.Picker;
import io.kredibel.picker.PickerListener;

import java.io.File;

public class MainActivity extends AppCompatActivity {

Expand All @@ -14,13 +22,26 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imgResult = findViewById(R.id.imgResult);
TextView txtPath = findViewById(R.id.txtPath);

findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera((uri, file, bitmap)
-> imgResult.setImageURI(uri)
));
findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera(new PickerListener() {
@Override
public void onPicked(Uri uri, File file, Bitmap bitmap) {
imgResult.setImageURI(uri);
txtPath.setText(file.getAbsolutePath()+"(" + (file.exists()) + ")");
Log.d("CEK", "CEK-EXIST: Bitmap|Camera: " + (bitmap != null));
Log.d("CEK", "CEK-EXIST: File|Camera: " + (file.exists()) + "|" + file.getAbsolutePath());
}
}));

findViewById(R.id.btnGalery).setOnClickListener(view -> picker.pickGallery((uri, file, bitmap)
-> imgResult.setImageURI(uri))
);
findViewById(R.id.btnGalery).setOnClickListener(view -> picker.pickGallery(new PickerListener() {
@Override
public void onPicked(Uri uri, File file, Bitmap bitmap) {
imgResult.setImageURI(uri);
txtPath.setText(file.getAbsolutePath()+"(" + (file.exists()) + ")");
Log.d("CEK", "CEK-EXIST: Bitmap|Gallery: " + (bitmap != null));
Log.d("CEK", "CEK-EXIST: File|Gallery: " + (file.exists())+ "|" + file.getAbsolutePath());
}
}));
}
}
10 changes: 9 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>

<TextView
android:id="@+id/txtPath"
android:layout_below="@+id/btnCamera"
android:layout_centerHorizontal="true"
android:text="Path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:id="@+id/imgResult"
android:layout_below="@+id/btnCamera"
android:layout_below="@+id/txtPath"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="10dp"/>
Expand Down
4 changes: 2 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.4-beta1"
version = "0.0.4-beta2"

publishing {

Expand Down Expand Up @@ -63,7 +63,7 @@ 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.3'
//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
120 changes: 31 additions & 89 deletions picker/src/main/java/io/kredibel/picker/PickerObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
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 @@ -24,7 +22,11 @@
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

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

import static android.app.Activity.RESULT_OK;
Expand All @@ -38,7 +40,9 @@ public class PickerObserver implements DefaultLifecycleObserver {
@Override
public void onActivityResult(Uri uri) {
if (uri != null) {
File file = new File(uri.getPath());
//File file = new File(uri.getPath());
Bitmap bitmap = uriToBitmap(uri);
File file = bitmapToFile(bitmap);
pickerListener.onPicked(uri, file, uriToBitmap(uri));
}
}
Expand All @@ -48,49 +52,47 @@ public void onActivityResult(Uri uri) {
public void onActivityResult(ActivityResult result) {
int resultCode = result.getResultCode();
Intent data = result.getData();
Uri uri = data.getData();

if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bundle bundle = Objects.requireNonNull(data).getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
//File file = compressor.compressToFile()

ContextWrapper cw = new ContextWrapper(activity.getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);

String child = new Random().nextInt(20) + System.currentTimeMillis() + "_picker" + ".jpg";

File file = new File(directory, child);
if (!file.exists()) {
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
File file = bitmapToFile(bitmap);
pickerListener.onPicked(Uri.fromFile(file), file, bitmap);
}
}
};
private ActivityResultLauncher<String> imageLauncher;
private ActivityResultLauncher<Intent> cameraLauncher;

public PickerObserver(@NonNull AppCompatActivity 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.registry = fr.requireActivity().getActivityResultRegistry();
}

File bitmapToFile(Bitmap bitmap) {
ContextWrapper cw = new ContextWrapper(activity.getApplicationContext());
File directory = cw.getDir("imgDir", Context.MODE_PRIVATE);

String child = new Random().nextInt(20) + System.currentTimeMillis() + "_Picker" + ".jpg";

File file = new File(directory, child);
if (!file.exists()) {
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return file;
}

@Override
public void onCreate(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onCreate(owner);
Expand Down Expand Up @@ -122,64 +124,4 @@ 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 b211fbe

Please sign in to comment.