diff --git a/app/src/main/java/id/co/kredibel/MainActivity.java b/app/src/main/java/id/co/kredibel/MainActivity.java index d8df9a6..13b1ba8 100644 --- a/app/src/main/java/id/co/kredibel/MainActivity.java +++ b/app/src/main/java/id/co/kredibel/MainActivity.java @@ -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 { @@ -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()); + } + })); } } \ 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 b93e168..c117b09 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -31,9 +31,17 @@ android:layout_marginTop="10dp" android:layout_centerHorizontal="true"/> + + diff --git a/picker/build.gradle b/picker/build.gradle index 1d12a79..25ff175 100644 --- a/picker/build.gradle +++ b/picker/build.gradle @@ -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 { @@ -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' diff --git a/picker/src/main/java/io/kredibel/picker/PickerObserver.java b/picker/src/main/java/io/kredibel/picker/PickerObserver.java index 98e42bb..6f3b319 100644 --- a/picker/src/main/java/io/kredibel/picker/PickerObserver.java +++ b/picker/src/main/java/io/kredibel/picker/PickerObserver.java @@ -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; @@ -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; @@ -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)); } } @@ -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 imageLauncher; private ActivityResultLauncher 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); @@ -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; - } - - }