How to read asset file?

Hi, im try to prgramm my first Extension, but i need some help, i want the user to be able to select an Asset in the Designer e.g.

@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_ASSET) @SimpleProperty(description = "Language File") public void langFile(String langFile) { this.langFile = langFile; }

Now i want to read that file

 if (MediaUtil.isExternalFile(path) &&
                container.$form().isDeniedPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) 
{
            container.$form().askPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
                new PermissionResultHandler() {
                    @Override
                    public void HandlePermissionResponse(String permission, boolean granted) {
                    if (granted) {
                        readJson(path);
                    } else {
                        container.$form().dispatchPermissionDeniedEvent(AutoI18N.this, "Json", permission);
                    }
                    }
                });
            return;
            }
            jsonPath = (path == null) ? "" : path;
            Gson gson = new Gson();
            JsonReader reader;
            try {
                reader = new JsonReader(new FileReader(jsonPath));
                Map trans = gson.fromJson(reader.toString(), Map.class);
                translations = gson.fromJson(trans.get("en").toString(), Map.class);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

but nothing happens, what am i doing wrong ?

Anyone here can help me ?
Its the only thing i cant get to work …
i now also tried something like this , but it still dont works …

private void readJson(final String path) {

        form.askPermission(Manifest.permission.READ_EXTERNAL_STORAGE, new PermissionResultHandler() {

            @Override

            public void HandlePermissionResponse(String permission, boolean granted) {

              if (granted) {

                try {

                    if (path.startsWith("//")) {

                        jsonPath = form.getAssetPath(path.substring(2));

                    } else {

                        jsonPath = AbsoluteFileName(path);

                    }

                    try (BufferedReader reader = new BufferedReader(new FileReader(jsonPath));)

                        {

                            Gson gson = new Gson();

                            

                            Map trans = gson.fromJson(reader.toString(), Map.class);

                            translations = gson.fromJson(trans.get("en").toString(), Map.class);

                        } catch (FileNotFoundException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }catch(JsonParseException e){

                        }

                   

                } catch (PermissionException e) {

                  form.dispatchPermissionDeniedEvent(AutoI18N.this, "ReadFrom", e);

                } catch (FileNotFoundException e) {

                } catch (IOException e) {

                }

              } else {

                form.dispatchPermissionDeniedEvent(AutoI18N.this, "ReadFrom", permission);

              }

            }

          });

    }

I don’t know exactly what your intent is, but to read a file from the assets you don’t need READ_EXTERNAL_STORAGE permission.
This is only required for files from external storage: /storage/emulated/0/...

2 Likes

Hi @Marggx,

You can do the following to get an InputStream for an asset:

InputStream in = null;
try {
  in = container.$form().openAsset(path);
} finally {
  IOUtils.closeQuietly(in);
}

If you’re looking to read an asset bundled into your extension, you should use Form’s openAssetForExtension method instead.

3 Likes

4 posts were split to a new topic: How to use @UsesAssets?