diff --git a/app/src/main/java/id/co/kredibel/MainActivity.java b/app/src/main/java/id/co/kredibel/MainActivity.java
index 13b1ba8..63a6cf0 100644
--- a/app/src/main/java/id/co/kredibel/MainActivity.java
+++ b/app/src/main/java/id/co/kredibel/MainActivity.java
@@ -4,12 +4,14 @@
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
+import android.view.View;
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 io.kredibel.picker.PickerObserver;
import java.io.File;
@@ -24,24 +26,20 @@ protected void onCreate(Bundle savedInstanceState) {
ImageView imgResult = findViewById(R.id.imgResult);
TextView txtPath = findViewById(R.id.txtPath);
- findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera(new PickerListener() {
+ PickerListener pickerListener = 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());
+ //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(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());
- }
- }));
+ findViewById(R.id.btnChoose).setOnClickListener(view -> picker.pickImage(pickerListener));
+
+ findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera(pickerListener));
+
+ findViewById(R.id.btnGalery).setOnClickListener(view -> picker.pickGallery(pickerListener));
}
}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index c117b09..a06ccf5 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -15,7 +15,7 @@
android:layout_centerHorizontal="true"/>
+
+
816 || width > 612) {
+ final int halfHeight = height / 2;
+ final int halfWidth = width / 2;
+ // Calculate the largest inSampleSize value that is a power of 2 and keeps both
+ // height and width larger than the requested height and width.
+ while ((halfHeight / inSampleSize) >= 816 && (halfWidth / inSampleSize) >= 612) {
+ inSampleSize *= 2;
+ }
+ }
+ return inSampleSize;
+ }
+
+ /*public FileCompressor setDestinationDirectoryPath(String destinationDirectoryPath) {
+ this.destinationDirectoryPath = destinationDirectoryPath;
+ return this;
+ }*/
+
+ public File compressToFile(File imageFile) throws IOException {
+ return compressToFile(imageFile, imageFile.getName());
+ }
+
+ public File compressToFile(File imageFile, String compressedFileName) throws IOException {
+ int quality = 50;
+ return compressImage(imageFile, compressFormat, quality, destinationDirectoryPath + File.separator + compressedFileName);
+ }
+
+ /*public Bitmap compressToBitmap(File imageFile) throws IOException {
+ return decodeSampledBitmapFromFile(imageFile);
+ }*/
+}
\ No newline at end of file
diff --git a/picker/src/main/java/io/kredibel/picker/Picker.java b/picker/src/main/java/io/kredibel/picker/Picker.java
index e1d7a3b..e465be2 100644
--- a/picker/src/main/java/io/kredibel/picker/Picker.java
+++ b/picker/src/main/java/io/kredibel/picker/Picker.java
@@ -37,4 +37,8 @@ public void pickCamera(PickerListener pickerListener) {
public void pickGallery(PickerListener pickerListener) {
observer.pickGallery(pickerListener);
}
+
+ public void pickImage(PickerListener pickerListener){
+ observer.pickImage(pickerListener);
+ }
}
diff --git a/picker/src/main/java/io/kredibel/picker/PickerObserver.java b/picker/src/main/java/io/kredibel/picker/PickerObserver.java
index 6f3b319..d6303c7 100644
--- a/picker/src/main/java/io/kredibel/picker/PickerObserver.java
+++ b/picker/src/main/java/io/kredibel/picker/PickerObserver.java
@@ -17,6 +17,7 @@
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
+import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
@@ -62,6 +63,7 @@ public void onActivityResult(ActivityResult result) {
};
private ActivityResultLauncher imageLauncher;
private ActivityResultLauncher cameraLauncher;
+
public PickerObserver(@NonNull AppCompatActivity activity) {
this.activity = activity;
this.registry = activity.getActivityResultRegistry();
@@ -90,7 +92,14 @@ File bitmapToFile(Bitmap bitmap) {
e.printStackTrace();
}
}
- return file;
+ File compresedFile;
+ FileCompressor fileCompressor = new FileCompressor(activity);
+ try {
+ compresedFile = fileCompressor.compressToFile(file);
+ } catch (IOException e) {
+ compresedFile = file;
+ }
+ return compresedFile;
}
@Override
@@ -124,4 +133,20 @@ public void pickCamera(PickerListener pickerListener) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(intent);
}
+
+ public void pickImage(PickerListener pickerListener) {
+ final CharSequence[] options = {"From Camera", "From Gallery", "Cancel"};
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ builder.setTitle("Pick Image!");
+ builder.setItems(options, (dialog, item) -> {
+ if (item == 0) {
+ pickCamera(pickerListener);
+ } else if (item == 1) {
+ pickGallery(pickerListener);
+ } else {
+ dialog.dismiss();
+ }
+ });
+ builder.show();
+ }
}