From acb052382ed4f6712349d11040dd783efcfcef1d Mon Sep 17 00:00:00 2001 From: agp8x Date: Mon, 30 Aug 2021 12:40:04 +0200 Subject: [PATCH] upload full image to minio --- .idea/misc.xml | 2 +- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 20 ++- .../android/miniofotoapp/MainActivity.java | 117 +++++++++++++++--- app/src/main/res/layout/activity_main.xml | 9 +- app/src/main/res/values/minio.xml | 7 ++ app/src/main/res/xml/file_paths.xml | 6 + 7 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 app/src/main/res/values/minio.xml create mode 100644 app/src/main/res/xml/file_paths.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 3d054a8..9988271 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ diff --git a/app/build.gradle b/app/build.gradle index ee9dc71..ad30d48 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,4 +36,6 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'io.minio:minio:8.3.0' + implementation 'javax.xml.stream:stax-api:1.0' + implementation 'com.fasterxml:aalto-xml:1.2.2' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index de599ca..0969fcd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,13 +6,22 @@ android:name="android.hardware.camera" android:required="true" /> + + + + + android:theme="@style/Theme.MinioFotoApp" + + android:usesCleartextTraffic="true" + > @@ -22,6 +31,15 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/de/clkl/android/miniofotoapp/MainActivity.java b/app/src/main/java/de/clkl/android/miniofotoapp/MainActivity.java index ccf5a51..3d1b288 100644 --- a/app/src/main/java/de/clkl/android/miniofotoapp/MainActivity.java +++ b/app/src/main/java/de/clkl/android/miniofotoapp/MainActivity.java @@ -2,21 +2,44 @@ package de.clkl.android.miniofotoapp; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.FileProvider; -import android.content.ActivityNotFoundException; import android.content.Intent; -import android.graphics.Bitmap; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.ImageView; +import java.io.File; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import io.minio.BucketExistsArgs; +import io.minio.MakeBucketArgs; +import io.minio.MinioClient; +import io.minio.UploadObjectArgs; +import io.minio.errors.ErrorResponseException; +import io.minio.errors.InsufficientDataException; +import io.minio.errors.InternalException; +import io.minio.errors.InvalidResponseException; +import io.minio.errors.MinioException; +import io.minio.errors.ServerException; +import io.minio.errors.XmlParserException; + public class MainActivity extends AppCompatActivity { static final int REQUEST_IMAGE_CAPTURE = 1; public static final String TAG = "DEMO"; private ImageView thumb; + private String currentPhotoPath; + private MinioClient minio; + private String bucket; @Override @@ -24,29 +47,95 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); thumb = findViewById(R.id.imageView); + minio = MinioClient.builder() + .endpoint(getString(R.string.minio_host)) + .credentials(getString(R.string.minio_access_key), getString(R.string.minio_secret_key)) + .build(); + bucket = getString(R.string.minio_bucket); + + checkOrCreateBucket(minio, bucket); } - private void dispatchTakePictureIntent(){ + private static void checkOrCreateBucket(MinioClient client, String bucket) { + Runnable check = new Runnable() { + @Override + public void run() { + + try { + boolean isPresent = false; + isPresent = client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()); + if (!isPresent) { + System.out.println("Create Bucket '" + bucket + "'"); + client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build()); + } else { + System.out.println("Bucket '" + bucket + "' already present"); + } + } catch (InvalidKeyException | IOException | NoSuchAlgorithmException | MinioException e) { + e.printStackTrace(); + Log.e(TAG, "something went wrong with minio", e); + } + } + }; + new Thread(check).start(); + } + + private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - try { - startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); - } catch (ActivityNotFoundException e) { - e.printStackTrace(); - Log.e(TAG, "Camera Activity not found", e); + if (takePictureIntent.resolveActivity(getPackageManager()) != null) { + File photoFile = null; + try { + photoFile = createImageFile(); + } catch (IOException e) { + e.printStackTrace(); + Log.e(TAG, "error creating photo file", e); + } + if (photoFile != null) { + Uri photoURI = FileProvider.getUriForFile( + this, + "de.clkl.android.MinioPhotoApp.fileprovider", + photoFile); + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); + startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); + } } } + private File createImageFile() throws IOException { + String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()); + String fileName = "JPEG_" + timeStamp + "_"; + File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); + File image = File.createTempFile( + fileName, + ".jpg", + storageDir + ); + currentPhotoPath = image.getAbsolutePath(); + return image; + } + @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (requestCode != REQUEST_IMAGE_CAPTURE) { super.onActivityResult(requestCode, resultCode, data); - }else{ - if(resultCode == RESULT_OK){ - Bundle extras = data.getExtras(); - Bitmap imageBitmap = (Bitmap) extras.get("data"); - thumb.setImageBitmap(imageBitmap); - } } + if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { + thumb.setImageURI(Uri.fromFile(new File(currentPhotoPath))); + Runnable upload = new Runnable() { + @Override + public void run() { + + try { + String filename = currentPhotoPath.substring(currentPhotoPath.lastIndexOf("/")); + minio.uploadObject(UploadObjectArgs.builder().bucket(bucket).object(filename).filename(currentPhotoPath).build()); + } catch (InvalidKeyException | IOException | NoSuchAlgorithmException | MinioException e) { + e.printStackTrace(); + Log.e(TAG, "something went wrong with minio", e); + } + } + }; + new Thread(upload).start(); + } + } public void onButtonClick(View view) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a9f540f..5d5be1e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -28,12 +28,15 @@ + app:srcCompat="@android:drawable/ic_dialog_alert" + android:adjustViewBounds="false" + android:scaleType="fitCenter" + /> \ No newline at end of file diff --git a/app/src/main/res/values/minio.xml b/app/src/main/res/values/minio.xml new file mode 100644 index 0000000..1a13863 --- /dev/null +++ b/app/src/main/res/values/minio.xml @@ -0,0 +1,7 @@ + + + demoAPI + 952fa5f2-0965-11ec-b846-1756113babc3 + http://192.168.2.53:9000 + android-demo + \ No newline at end of file diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..fa29695 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file