Skip to content

Commit

Permalink
addons: capture and add an image to resources (fixes #2370) (#2398)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Sep 15, 2023
1 parent 69cfab7 commit de852bb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class AddResourceFragment extends BottomSheetDialogFragment {
FloatingActionButton floatingActionButton;
AudioRecorderService audioRecorderService;
File output;
private Uri photoURI;
private Uri videoUri;

public AddResourceFragment() {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

0 comments on commit de852bb

Please sign in to comment.