3

I'm trying to develop a File Chooser for an app developed in Ionic, the intention of this function is to read big files. I tried using Capacitor plugin, however the app crashes with files above 25mb, depending on cell phone. Through Memory Profiler I understand that it's because of the high memory usage of the Chooser when selecting a file, that is why I'm trying to create my own Chooser to control memory usage.

I'm using Capacitor itself to create custom plugins.

I started my development with some examples I found on Stack Overflow. I started getting some errors with startActivityForResult() function. From the Activity Lifecycle documentation I tried to create new Activities and get their answers. It was possible to validate that the developed code works because the startActivity() function opened the File Chooser, but I still need to get the Uri from the file. Using the startActivityForResult() function I can't even open the File Chooser.

Even creating a new Activity there was still an error in the Activity Lifecycle. The error was something that look like this. I rewrote my code based on Android documentation about Intents. Using everything I had gathered, the code looked like this:

public class ReadFile extends Activity {
    public void openFile(String mimetype) {
        try {
          mGetContent.launch(mimeType);
        } catch (Exception e) {
          System.out.println(e);
        }
    }

    ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
      new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
          System.out.println("uri " + uri);
        }
    }); 
}

The IDE told me that the registerForActivityResult() function didn't exist in my class, so I found an answer that helped with this problem. I solved this problem by declaring a new Fragment in my Class.

public class ReadFile extends Activity {

    private Fragment fragment = new Fragment();

    public void openFile(String mimetype) {
        try {
          mGetContent.launch(mimeType);
        } catch (Exception e) {
          System.out.println(e);
        }
    }

    ActivityResultLauncher<String> mGetContent = fragment.registerForActivityResult(new ActivityResultContracts.GetContent(),
      new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
          System.out.println("uri " + uri);
        }
    });

}

Another error appeared: java.lang.IllegalStateException: Operation cannot be started before Fragment is in created state. I found an interesting discussion on a Google forum, within the discussion it was commented that the problem could be the Fragment's Lifecycle. I then read the Fragment Lifecycle documentation and tried to apply its methods.

private MyLocationListener myLocationListener;

@Override
public void onCreate(...) {
    myLocationListener = new MyLocationListener(this, (location) -> {
        // update UI
    });
}

@Override
public void onStart() {
    super.onStart();
    myLocationListener.start();
    // manage other components that need to respond
    // to the activity lifecycle
}

@Override
public void onStop() {
    super.onStop();
    myLocationListener.stop();
    // manage other components that need to respond
    // to the activity lifecycle
}

Even applying these Lifecycle methods I couldn't get the File Chooser to work. My goal is to get the data from the selected file through the Android File Chooser.

Gabi Vieira
  • 371
  • 2
  • 13

0 Answers0