lovelyelfpop opened a new issue, #526: URL: https://github.com/apache/cordova-plugin-file/issues/526
# Bug Report ## Problem Android webview cannot display `<img>` with src="`https://localhost/__cdvfile_content__/...`" ### What is expected to happen? Android webview should display `<img>` with src="`https://localhost/__cdvfile_content__/...`" ### What does actually happen? ERR_CONNECTION_REFUSED in console,img not displaying ## Information `<img>` with src like `https://localhost/__cdvfile_asstes__/...` or `https://localhost/__cdvfile_files__/...` can display in webview, but not `https://localhost/__cdvfile_content__/...`, ### Environment, Platform, Device Redmi K30 pro, Android 11 ### Version information [email protected] [email protected] [email protected] ## Checklist <!-- Please check the boxes by putting an x in the [ ] like so: [x] --> - [x] I searched for existing GitHub issues - [x] I updated all Cordova tooling to most recent version - [x] I included all the necessary information above ## Reason `WebViewAssetLoader.PathHandler` in `cordova-plugin-file\src\android\FileUtils.java` missing `content` uri handler. The right code is ```java public CordovaPluginPathHandler getPathHandler() { WebViewAssetLoader.PathHandler pathHandler = path -> { String targetFileSystem = null; if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("persistent"))) { targetFileSystem = "persistent"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("temporary"))) { targetFileSystem = "temporary"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("files"))) { targetFileSystem = "files"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("documents"))) { targetFileSystem = "documents"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache"))) { targetFileSystem = "cache"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("root"))) { targetFileSystem = "root"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("files-external"))) { targetFileSystem = "files-external"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("sdcard"))) { targetFileSystem = "sdcard"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache-external"))) { targetFileSystem = "cache-external"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("assets"))) { targetFileSystem = "assets"; } // --------------------added start-------------------- else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("content"))) { targetFileSystem = "content"; } // --------------------added end-------------------- boolean isAssetsFS = targetFileSystem == "assets"; if (targetFileSystem != null) { // Loop the registered file systems to find the target. for (Filesystem fileSystem : filesystems) { /* * When target is discovered: * 1. Transform the url path to the native path * 2. Load the file contents * 3. Get the file mime type * 4. Return the file & mime information back we Web Resources */ if (fileSystem.name.equals(targetFileSystem)) { // E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/" String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", ""); String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri); File file = null; if (isAssetsFS) { fileTarget = fileTarget.replace("/android_asset/", ""); } else { file = new File(fileTarget); } try { // --------------------added start-------------------- if(targetFileSystem == "content") { ContentResolver cr = webView.getContext().getContentResolver(); Uri uri = Uri.parse(fileTarget); InputStream fileIS = new FileInputStream(cr.openFileDescriptor(uri, "r").getFileDescriptor()); String fileMimeType = cr.getType(uri); return new WebResourceResponse(fileMimeType, null, fileIS); } // --------------------added end-------------------- InputStream fileIS = !isAssetsFS ? new FileInputStream(file) : webView.getContext().getAssets().open(fileTarget); String filePath = !isAssetsFS ? file.toString() : fileTarget; Uri fileUri = Uri.parse(filePath); String fileMimeType = getMimeType(fileUri); return new WebResourceResponse(fileMimeType, null, fileIS); } catch (FileNotFoundException e) { Log.e(LOG_TAG, e.getMessage()); } catch (IOException e) { Log.e(LOG_TAG, e.getMessage()); } } } } return null; }; return new CordovaPluginPathHandler(pathHandler); } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
