1

I have a mobile app that wraps around the web-app, using webview.

The web-app has a button to open a large .zip file (e.g. 100 MB).
The user clicks a button, and selects a .zip file.
This triggers an onChange function with a variable of type File (Blob), which includes attributes like:

  • file name
  • file size
  • file type (application/zip)

The javascript code then parses the .zip file, extracts specific data within it and uses it within the web-app.

This works well within the web-app, when the app is called via the Chrome browser.
For example when operated in chrome browser on an Android phone, I can pull the .zip file and open it in the web-app.

I want to do the same but using the mobile app.
I am able to pick up the .zip file using a File Chooser, but I have problems to fetch the file from the Javascript code.

When calling fetch(fileUri) from the Javascript side I'm getting errors. I'm using the following uri /storage/emulated/0/Android/data/com.example/files/Download/file2.zip
The fetch succeeds but returns a blob with size of 165 (i.e. not the actual size of the file) which hosts the error message:

{
      "error": "Not Found", 
      "message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."
}

The program flow is like so:

  • I select a .zip file via FileChooser.
  • In onActivityResult, the uri value is /document/msf:12858 (seen via uri = intent.getData();)
  • The uri needs to be mapped into a real path file url, such that the fileUrl will be passed to Javascript (via webview).
  • Javascript will then fetch the file using the fileUrl.

I searched how to get the real path file url when selecting a file with FileChooser, and found this, and this links.

I wasn't able to get the real file path, so I decided to read the file and write it to another location, so I can get a file path. (this is not efficient and done just to check the functionality).

I create the new file using the following code:

InputStream stream = context.getContentResolver().openInputStream(uri);
File file2 = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "file2.zip");
writeBytesToFile(stream, file2);

I don't see any errors when creating the file, and when creating the file, the number of bytes that are read and written to the new file are as expected.
For file2, I get a value of:
/storage/emulated/0/Android/data/com.example/files/Download/file2.zip

Then, within the Javascript code I fetch this file path.
But I'm getting a Blob with the "file-not-found" content as above.

So:

  • How can I verify that the file is indeed created and that the path can be fetched from Javascript?
  • How can I get the real file path of the original selected file, so I don't have to read and write the original file to new location just to get the file path?

Thanks


EDIT1:

I have "Intent intent" and not "Intent data" in the signature of onActivityResult.
I replaced String filePath = intent.getData() with

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        try {
            intent_to_resolve = new JSONObject();
            if (resultCode == RESULT_OK) {

                // try1
                String filePath1 = intent.getData().toString();
                Log.d("Verbose", "filePath1: " + filePath1);

                // try2
                String filePath2 = intent.toString();
                Log.d("Verbose", "filePath2: " + filePath2);
    ...

I'm getting the following value for filePath:

filePath1: content://com.android.providers.downloads.documents/document/msf%3A12858
filePath2: Intent { dat=content://com.android.providers.downloads.documents/document/msf:12858 flg=0x43 }
Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/238556/discussion-on-question-by-avner-moshkovitz-android-webview-how-to-pass-a-large). – Machavity Oct 26 '21 at 13:57

0 Answers0