-1

I am struggling to find the "Context" value in call this function like

SetFile("Name", ???, 1, "Some text here")

I have looked here https://developer.android.com/reference/android/content/Context but there seems to be a lot of possibilities and not clear to me which one I should use and how. I have no idea what to fill in the "???" place, can anybody help me out on this?

public void SetFile(string BroadCastName, Context Context, long? DirectoryId, string BaseContent)
    {
        if (!BroadCastName.Contains(".enc"))
            BroadCastName = BroadCastName + ".enc";
        if (DirectoryId == null)
            DirectoryId = 2;
        var File = Directories.Where(x => x.Id == DirectoryId).FirstOrDefault().File;
        var Path = Directories.Where(x => x.Id == DirectoryId).FirstOrDefault().Path;
        File FilesDir = new File(Path, "SDDnode");
        FilesDir.Mkdir();
        if (bool.Equals(FilesDir.CanWrite(), true)) {
            File newFile = new File(FilesDir, BroadCastName);
            try
            {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Context.OpenFileOutput(BroadCastName, FileCreationMode.Private));
                outputStreamWriter.Write(BaseContent);
                outputStreamWriter.Flush();
                outputStreamWriter.Close();
            }
            catch (IOException e)
            {
                #region
                ledger._base.Errors.Add(new Error {
                    Message = "Something went wrong in generate file in Android",
                    Process = "sddnode_android.Base._system",
                    Sys_Message = e.Message,
                    Line = 115,
                    Priority = 1
                });
                #endregion
            }

            OutputStream os;
            try
            {
                os = new FileOutputStream(newFile);
                //target.compress(CompressFormat.PNG, 100, os);
                os.Flush();
                os.Close();
            }
            catch (FileNotFoundException e)
            {
                e.PrintStackTrace();
            }
            catch (IOException e)
            {
                e.PrintStackTrace();
            }
        }
user3763117
  • 327
  • 1
  • 5
  • 18

3 Answers3

1

For that piece of code, there's really no way to find out where that Context came from, since we don't know what called it.

But, the highest possibility is an Activity context, due to the type of method it is, though a Service is also likely.

However, it really doesn't matter, since the only use of Context in that method is Context.OpenFileOutput(BroadCastName, FileCreationMode.Private) and this is something that can be done safely with a context from anywhere.

Edit:

To get the Context, you can simply use the this keyword inside an Activity or Service.

So wherever you want to run that method, you simply call SetFile("test", MainActivity.this, 2, ""); Replace MainActivity with the class name of the Activity or Service you're using it in.

If you're using it in some other place outside of an Activity or Service, check to see if that component has a getContext() method.

Jackey
  • 3,184
  • 1
  • 11
  • 12
  • the call be like this SetFile("test", Context.AppOpsService, 2, ""); Can you help me out and give a code example? – user3763117 Jan 15 '19 at 12:28
  • I don't understand what you're asking. Are you asking where to get the Context to use in the method? – Jackey Jan 15 '19 at 12:33
0

You can use the context of the activity that is calling the SetFile method.

You can get a good explanation of the context class in here.

npeixoto
  • 16
  • 3
  • Yes I saw this post but it still does not give me the sample or answer I need as its very confusing, I look for answer like this "SetFile("test", Context.AppOpsService, 2, "");" – user3763117 Jan 15 '19 at 12:27
0

You can get application context using

var currentContext = Android.App.Application.Context;

and then use it to pass in setFile method. Like,

SetFile("Name",currentContext, 1, "Some text here")
Abhay Koradiya
  • 2,068
  • 2
  • 15
  • 40