From de852bbf9b508818e83bdda9ef8ab366420942cb Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Fri, 15 Sep 2023 19:52:17 +0300 Subject: [PATCH] addons: capture and add an image to resources (fixes #2370) (#2398) Co-authored-by: dogi --- .github/dependabot.yml | 4 +- app/build.gradle | 4 +- .../ui/library/AddResourceFragment.java | 28 +++++------ .../ui/viewer/ImageViewerActivity.java | 50 +++++++++++++++---- 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index bf665f74f1..9436e87d36 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -8,9 +8,9 @@ updates: - package-ecosystem: "github-actions" directory: "/" schedule: - interval: "weekly" + interval: "daily" - package-ecosystem: "gradle" directory: "/" schedule: interval: "daily" - open-pull-requests-limit: 10 + open-pull-requests-limit: 12 diff --git a/app/build.gradle b/app/build.gradle index f5dd57850c..e918af5c14 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1037 - versionName "0.10.37" + versionCode 1038 + versionName "0.10.38" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/library/AddResourceFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/library/AddResourceFragment.java index 15c3c4d149..4049eab498 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/library/AddResourceFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/library/AddResourceFragment.java @@ -56,6 +56,7 @@ public class AddResourceFragment extends BottomSheetDialogFragment { FloatingActionButton floatingActionButton; AudioRecorderService audioRecorderService; File output; + private Uri photoURI; private Uri videoUri; public AddResourceFragment() { @@ -190,31 +191,30 @@ private Uri createVideoFileUri() { return videoUri; } public void takePhoto() { - Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); - output = new File(dir, UUID.randomUUID().toString() + ".jpg"); + ContentValues values = new ContentValues(); + values.put(MediaStore.Images.Media.TITLE, "Photo_" + UUID.randomUUID().toString()); + values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); - // Generate a content URI using FileProvider - Uri photoURI = FileProvider.getUriForFile(requireContext(), "org.ole.planet.myplanet.fileprovider", output); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/ole/photo"); + } - // Grant temporary permission to the camera app to access the file - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + photoURI = requireActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); - // Set the output URI + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); - startActivityForResult(intent, REQUEST_CAPTURE_PICTURE); + + if (intent.resolveActivity(requireActivity().getPackageManager()) != null) { + startActivityForResult(intent, REQUEST_CAPTURE_PICTURE); + } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Uri uri = null; -// String path = ""; if (requestCode == REQUEST_CAPTURE_PICTURE) { -// if (output != null) { -// url = Uri.fromFile(output); -// path = url.getPath(); -// } + uri = photoURI; } else if (requestCode == REQUEST_VIDEO_CAPTURE) { uri = videoUri; } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/viewer/ImageViewerActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/viewer/ImageViewerActivity.java index b771316907..72818b9906 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/viewer/ImageViewerActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/viewer/ImageViewerActivity.java @@ -7,14 +7,19 @@ import androidx.appcompat.app.AppCompatActivity; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; +import com.bumptech.glide.request.RequestOptions; import org.ole.planet.myplanet.databinding.ActivityImageViewerBinding; import java.io.File; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class ImageViewerActivity extends AppCompatActivity { private ActivityImageViewerBinding activityImageViewerBinding; private boolean isFullPath = false; + String fileName; @Override protected void onCreate(Bundle savedInstanceState) { @@ -27,23 +32,46 @@ protected void onCreate(Bundle savedInstanceState) { private void renderImageFile() { isFullPath = getIntent().getBooleanExtra("isFullPath", false); Intent imageOpenIntent = getIntent(); - String fileName = imageOpenIntent.getStringExtra("TOUCHED_FILE"); + fileName = imageOpenIntent.getStringExtra("TOUCHED_FILE"); if (fileName != null && !fileName.isEmpty()) { activityImageViewerBinding.imageFileName.setText(fileName); activityImageViewerBinding.imageFileName.setVisibility(View.VISIBLE); } - try { - File imageFile; - if (isFullPath) { - imageFile = new File(fileName); - } else { - File basePath = getExternalFilesDir(null); - imageFile = new File(basePath, "ole/" + fileName); + if (fileName.matches(".*[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}.*")) { + displayCapturedImage(); + } else { + try { + File imageFile; + if (isFullPath) { + imageFile = new File(fileName); + } else { + File basePath = getExternalFilesDir(null); + imageFile = new File(basePath, "ole/" + fileName); + } + Glide.with(getApplicationContext()).load(imageFile).into(activityImageViewerBinding.imageViewer); + + } catch (Exception e) { + e.printStackTrace(); } - Glide.with(getApplicationContext()).load(imageFile).into(activityImageViewerBinding.imageViewer); - } catch (Exception e) { - e.printStackTrace(); } } + + private void displayCapturedImage() { + Pattern uuidPattern = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/"); + Matcher matcher = uuidPattern.matcher(fileName); + + if (matcher.find()) { + fileName = fileName.substring(matcher.group().length()); + } + + RequestOptions requestOptions = new RequestOptions() + .diskCacheStrategy(DiskCacheStrategy.NONE) + .skipMemoryCache(true); + + Glide.with(this) + .load(fileName) + .apply(requestOptions) + .into(activityImageViewerBinding.imageViewer); + } }