Plugin for saving a file

I’m trying to make a test plugin for saving a file. I have posted a thread where I wanted a solution for saving image to the gallery. It works perfectly (LOTS of thanks to him), but before that I tried to make it by myself and now it is just interesting for me where I’m wrong in my plugin.

Here is my plugin code:

package testpackage;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import java.io.FileOutputStream;
import java.io.File;

public class TestClass {
    public static String saveFile(Activity activity, String str) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "12345.txt");

        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write(str.getBytes());
            out.close();
        }
        catch(Exception e) {
            return "error";
        }
       
        return file.getPath() + " " + file.exists();
    }
}

How I’m using my plugin in Unity:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public static class TestPluginScript {
    static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    static AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    public static string getMessage() {
        AndroidJavaClass pluginClass = new AndroidJavaClass("testpackage.TestClass");
        return pluginClass.CallStatic<string>("saveFile", activity, "12345");
    }
}

Plugin returns valid path and file.exists() returns true. But there is no file created in that directory… Where is my mistake? Thanks.

Update… I don’t know why, but I CAN see it from device, but I can’t see it when opening folder from computer. How?

I found a solution. After creating a file one has to call the “MediaScannerConnection.scanFile(…)” function. Hope it will help somebody with a similar problem. :slight_smile: