--- ###

I'm new to app development.
After the screen transition from MainActivity
Link content
I was able to get an image from the gallery and mark it by referring to this article.
After that, I would like to load the image into opencv and perform image processing.
mat_src = Imgcodecs.imread ()
I think I put the file name in imread at this time, but I don't know the file name of the acquired image. mbitmap.compress (Bitmap.CompressFormat.JPEG, 100, fos);
This mbitmap part will inevitably result in an error.

Corresponding source code
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SettingActivity extends AppCompatActivity {
    private static final int REQUEST_CHOOSER = 1000;
    private Uri m_uri;
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_setting);
        Button returnButton = findViewById (R.id.button2);
        returnButton.setOnClickListener (new View.OnClickListener () {
            @Overridepublic void onClick (View v) {
                finish ();

        // Call camera Intent
        String photoName = System.currentTimeMillis () + ".jpg";
        ContentValues ​​contentValues ​​= new ContentValues ​​();
        contentValues.put (MediaStore.Images.Media.TITLE, photoName);
        contentValues.put (MediaStore.Images.Media.MIME_TYPE, "image/*");
        m_uri = getContentResolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        Intent intentCamera = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
        intentCamera.putExtra (MediaStore.EXTRA_OUTPUT, m_uri);
        // Create an Intent for the gallery
        Intent intentGallery;
        if (Build.VERSION.SDK_INT<19) {
            intentGallery = new Intent (Intent.ACTION_GET_CONTENT);
            intentGallery.setType ("image/*");
        } else {
            intentGallery = new Intent (Intent.ACTION_OPEN_DOCUMENT);
            intentGallery.addCategory (Intent.CATEGORY_OPENABLE);
            intentGallery.setType ("image/jpeg");
        Intent intent = Intent.createChooser (intentCamera, "Select image");
        intent.putExtra (Intent.EXTRA_INITIAL_INTENTS, new Intent [] {intentGallery});
        startActivityForResult (intent, REQUEST_CHOOSER);
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (requestCode == REQUEST_CHOOSER) {
            if (resultCode! = RESULT_OK) {
                // At the time of cancellation
            Uri resultUri = (data! = null? data.getData (): m_uri);
            if (resultUri == null) {
                // Acquisition failure
            MediaScannerConnection.scanFile (
                    new String [] {resultUri.getPath ()},

                    new String [] {"image/jpeg"},

            // Set image
            ImageView imageView = (ImageView) findViewById (R.id.imageView);
            imageView.setImageURI (resultUri);

        try {
            // Specify sdcard folder
            File root = Environment.getExternalStorageDirectory ();
            // Create a file name by date
            Date mDate = new Date ();
            SimpleDateFormat fileName = new SimpleDateFormat ("yyyyMMdd_HHmmss");
            // Start saving process
            FileOutputStream fos = null;
            fos = new FileOutputStream (new File (root, fileName.format (mDate) + ".jpg"));
            // Save as jpeg
            bitmap.compress (Bitmap.CompressFormat.JPEG, 100, fos);
            // End of save process
            fos.close ();
            // Load into OpenCV
            Mat mat_src = new Mat ();
            mat_src = Imgcodecs.imread (fileName.format (mDate) + ".jpg");
        } catch (Exception e) {
            Log.e ("Error", "" + e.toString ());
What I tried

I tried to include the one made by date in the file name, but I got an error in the save process.

  • Answer # 1

    This article
    I was able to do it like