upload full image to minio

master
agp8x 2021-08-30 12:40:04 +02:00
parent 13f9e891d3
commit acb052382e
7 changed files with 144 additions and 19 deletions

View File

@ -3,7 +3,7 @@
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="app/src/main/res/layout/activity_main.xml" value="0.19444444444444445" />
<entry key="app/src/main/res/layout/activity_main.xml" value="0.1" />
</map>
</option>
</component>

View File

@ -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'
}

View File

@ -6,13 +6,22 @@
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MinioFotoApp">
android:theme="@style/Theme.MinioFotoApp"
android:usesCleartextTraffic="true"
>
<activity
android:name=".MainActivity"
android:exported="true">
@ -22,6 +31,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="de.clkl.android.MinioPhotoApp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>

View File

@ -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) {

View File

@ -28,12 +28,15 @@
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="40dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button"
app:srcCompat="@android:drawable/ic_dialog_alert" />
app:srcCompat="@android:drawable/ic_dialog_alert"
android:adjustViewBounds="false"
android:scaleType="fitCenter"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="minio_access_key">demoAPI</string>
<string name="minio_secret_key">952fa5f2-0965-11ec-b846-1756113babc3</string>
<string name="minio_host">http://192.168.2.53:9000</string>
<string name="minio_bucket">android-demo</string>
</resources>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="my_minio_images"
path="Pictures" />
</paths>