Saving Mat Image in Android Gallery

asked 2016-09-12 06:53:53 -0500

WhoAmI gravatar image

I want to save the Mat Images in android gallery automatically. When I run my code in an Emulator the resultant Mat Image is not saving in Gallery.

The code for saving Images is below

String Folder = Environment.getExternalStorageDirectory().getPath()+"/Gallery";
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").Format(new Date());
Highgui.imwrite(Folder + "/" + "Name_" + timestamp + ".png", outputImage);

--It didn't worked and so I searched for an answer in this Forum and tried the answer from the below post

> //subimg -> your frame

    Bitmap bmp = null;
    try {
        bmp = Bitmap.createBitmap(subimg.cols(), subimg.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(subimg, bmp);
    } catch (CvException e) {
        Log.d(TAG, e.getMessage());


    FileOutputStream out = null;

    String filename = "frame.png";

    File sd = new File(Environment.getExternalStorageDirectory() + "/frames");
    boolean success = true;
    if (!sd.exists()) {
        success = sd.mkdir();
    if (success) {
        File dest = new File(sd, filename);

        try {
            out = new FileOutputStream(dest);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
            // PNG is a lossless format, the compression factor (100) is ignored

        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        } finally {
            try {
                if (out != null) {
                    Log.d(TAG, "OK!!");
            } catch (IOException e) {
                Log.d(TAG, e.getMessage() + "Error");

It too didn't worked out for me.

I searched for the permissions in AndroidManifest file and it is fine.

Any words from you......

edit retag flag offensive close merge delete