Save a file in the app folder using extension

Hi,
I am creating an extension and one task is save a file in the app folder here the code that I use:

In the app folder i don't find the file, the DefaultFileScope is App
if I use
File pngFile = new File("sdcard/TeamLogo.png");
instead of
File pngFile = new File(activity.getFilesDir(), fileName);
the file is createad correctly

Thank You for help
Best Regards
Marco

Pass null as argument.

If you are interested more in working with files, please have a look.

Hi @vknow360,
sorry is not clear, for me, when You say:
activity.getFilesDir()
Pass null as argument
what I should do?
Thank You
Marco

Sorry my mistake.
I misinterpreted getFilesDir as getExternalFilesDir.

It also creates files but you can't view it normally in File Manager. It is stored in Internal Storage which is viewable only with root.

This is the path where I try to store the file but I can't see the image, so I don't know if the image is correctly saved or what

codeLogo

Here is some example code I have that returns an asset file (as a tempFile - in that same directory), you will see that this is all done "inside" the extension

@SimpleFunction(description = "Returns asset as File. This works only for a compiled app. Your file must have an extension, and there should be no 'dots' " +
          "in the file other than the one that " +
          "separates the name from the extension")
  public String GetAsset(String filename) throws IOException {

    File file = File.createTempFile("temp", ("." + filename.split("\\.")[1].toLowerCase()));
    filePath = file.getAbsolutePath();

    InputStream in = context.getAssets().open(filename);
    int size = in.available();
    byte[] buffer = new byte[size];
    in.read(buffer);
    in.close();

    FileOutputStream out = new FileOutputStream(filePath);
    out.write(buffer);
    out.close();

    return filePath;
  }

image

Hopefully this doesn't serve to further confuse :wink:

1 Like

Hi @TIMAI2 ,
thank You for your help, in my case
int size = in.available(); is 0
I suppose because my InputStream is = httpConn.getInputStream
and in your case
InputStream in = context.getAssets().open(filename);

Anyway, I found the problem, the file was create correctly in the ASD folder with my extension but to display the image if I use the absolute path:
data/user/0/app//files/fileName.png
the file is not shown in the image component
Instead if I use the full path:
file:///data/user/0/app//files/fileName.png
The file is correctly shown int the image component.

Best Regards
Marco Perrone

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.