Java Extension for AI Error - FileInputStream

Hi,

I have been working on an extension for an AI application, and I have zeroed in on what has been an error I have not been able to resolve, in executing the extension, so I'm hoping I can find some useful help here. Note that for the Java code in the extension, the same lines run successfully in a Java app, so this is a bit confusing.

The error in the extension comes from the execution of "FileInputStream", which I use with only a single string input, which is the path to a .json file. In AI, this file is housed in the basic upload default directory for the project (docs suggests another way which is to pass both a form and filepath string). I know enough to know that all files uploaded are not treated the same, since we know that for images in most blocks, we can usually specify only the file name, while for webviews file paths, for example, we have to distinguish between development or application, etc. I cannot find anything reliable that discusses any differences we should expect for use of this method, so I am hoping some out there may have some ideas. Below is the minimal extension block that throws the error in question(please excuse the imperfect syntax convention at this point). Any information or ideas on how I can resolve this and effectively execute this command, would be great. I also have a simple AI app with the compiled extension included to test, if that is helpful. I do not see an option to share such a thing now, though. Thanks everyone.

Extension Java code:

/////////////////////////////////////////////////////////////////////////
    package com.testFileInputStream;

    import com.google.appinventor.components.annotations.DesignerComponent;
    import com.google.appinventor.components.annotations.DesignerProperty;
    import com.google.appinventor.components.annotations.PropertyCategory;
    import com.google.appinventor.components.annotations.SimpleEvent;
    import com.google.appinventor.components.annotations.SimpleFunction;
    import com.google.appinventor.components.annotations.SimpleObject;
    import com.google.appinventor.components.annotations.SimpleProperty;
    import com.google.appinventor.components.common.ComponentCategory;
    import com.google.appinventor.components.common.PropertyTypeConstants;
    import com.google.appinventor.components.runtime.util.ErrorMessages;
    import com.google.appinventor.components.runtime.Form;
    import com.google.appinventor.components.runtime.util.*;
    import com.google.appinventor.components.runtime.*;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.concurrent.ExecutionException;
    import java.io.File;
    ///
    import android.util.Log;
    import android.content.Context;


    @DesignerComponent(version = testFileInputStream.VERSION,
        description = " Testing FileInputStream ",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")
    @SimpleObject(external = true)
    public class testFileInputStream extends AndroidNonvisibleComponent implements Component {

        public static final int VERSION = 1;    
        private ComponentContainer container;
    	private Context context;
    	private static final String LOG_TAG = "testFileInputStream";
    	private String filePath;
    	private String readStatus;
    	
    	
      public static final String DEFAULT_EXAMPLE_VAR_VALUE = "default";    
     
        public testFileInputStream(ComponentContainer container) {
            super(container.$form());
            this.container = container;
            this.context = container.$context();
        }
    	
    	@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,  // change to what type of variable you have
                        defaultValue = testFileInputStream.DEFAULT_EXAMPLE_VAR_VALUE)
    	
    	@SimpleProperty(
          description = "Test variable")
    		public void readIt(String inVar) {
    		this.readStatus = inVar;
    	}  
    	
      
      // Function for testing FileInputStream.
        @SimpleFunction(description = "Testing FileInputStream")
    	public static String testFileInputStream(String filePath) throws IOException, ExecutionException, InterruptedException {
    	   String successRead = "False";   
    	  
    	   FileIntputStream readData = new FileInputStream(filePath);
    	   successRead = "True";
    	   		
    	//...	
    		return successRead;

        }	
     } 
//////////////////////////////////////////////////////////////

can you share the error,

And try adding this line :
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.ByteArrayInputStream;

Hi,

Yes...sorry I forgot to include that. The error from AP in both dev and app versions is:

'file.json': open failed: ENOENT (No such file or directory)

'file.json' is the file that is passed to FileInputStream. I'll try your suggestion, too, and report back. Thank you!
Thanks.

BTW, the typo in FileIntputStream readData... is not in the actual code...sorry if that confused anyone.

Still having the same error after adding those 3 lines :O(...

Please show us your build error

The error is runtime, thrown when I press a button that includes the call to FileInputStream. I don't have any build errors. The extension builds successfully, and is loaded into the AI app.

The error is : 'file.json': open failed: ENOENT (No such file or directory) where 'file.json' is the file path passed to FileInputStream. Does this answer your question?

Make sure:

  1. You have read permission
  2. File/Dir exists

Before posting this, I did test 1. by adding the block to ask permissions when screen1 loads, but unfortunately, this did not change anything.

The second one is basically the mystery I'm trying to solve, because I know the json file is uploaded and sitting inside the project, however, I don't know if AI represents this file some particular way, as it does with image files and/or html files for webviewers, etc. I have tried the explicit file name with no pre-text as in the example shown, and also with "/", "//", "android_asset/", "mnt/sdcard/AppInventor/assets/", and "storage/emulated/0/", but none of them resolved the error. So, to me, I know the file is in the project, but I don't know where AI thinks the file is located. The expected possibilities are not working...