WebView加载文件在Android上不起作用,但在iOS上起作用。

huangapple go评论66阅读模式
英文:

WebView loadFile not working on Android, But works on IOS

问题

以下是您提供的代码的翻译部分:

加载进度已经达到100%,但没有显示文件。

尝试更改路径
尝试从西里尔字母转换为拉丁字母
尝试externalStorageDir

@override
void initState() {
  controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..enableZoom(true)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          printMessage("viewer progress : $progress");
        },
        onPageStarted: (String url) {},
        onPageFinished: (String url) {},
        onWebResourceError: (WebResourceError error) {
          setState(() {
            canRenderFile = false;
          });
          printMessage("error viewer ${error.description}");
        },
      ),
    );
  super.initState();
}

@override
Widget build(BuildContext context) {
  printMessage(widget._path ?? "");
  if (canRenderFile) {
    controller.loadFile(widget._path ?? "");
  }
  if (canRenderFile == true) {
    return WebViewWidget(controller: controller);
  } else {
    return DownloadFileView(
      fileName: widget.file?.name ?? "",
      fileSize: widget.file?.fileSize.toUiSize().toString() ?? "",
      onAction: () {
        widget.onDownload?.call();
      },
    );
  }
}
}

这是我的代码
canRenderFile默认为true

请注意,代码中的"被翻译成了正常的双引号 ".

英文:

WebView加载文件在Android上不起作用,但在iOS上起作用。

Load progress is equal 100 but not showing file.

Tried to change path
Tried change from Cyrillic to Latin
Tried externalStorageDir

  @override
  void initState() {
    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..enableZoom(true)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            printMessage("viewer progress : $progress");
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {
            setState(() {
              canRenderFile = false;
            });
            printMessage("error viewer ${error.description}");
          },
        ),
      );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    printMessage(widget._path ?? "");
    if (canRenderFile) {
      controller.loadFile(widget._path ?? "");
    }
    if (canRenderFile == true) {
      return WebViewWidget(controller: controller);
    } else {
      return DownloadFileView(
        fileName: widget.file?.name ?? "",
        fileSize: widget.file?.fileSize.toUiSize().toString() ?? "",
        onAction: () {
          widget.onDownload?.call();
        },
      );
    }
  }
}

This is my code
canRenderFile is default = true

答案1

得分: 1

在Android上,如果您尝试使用WebView来显示任何文件(例如PDF),那么它将不显示任何内容。进度将显示100%,不会出现错误,但内容不会显示。因此,在这里,您将不得不使用某个库来预览文件,或者只需编写一些代码来显示系统弹出窗口,显示查看文件的选项。

英文:

On android , if you will try to use webview to show any file (for ex PDF) then it will not show anything .. progress will show 100% and no error will be there but content will not show . So here you will have to use either some library to preview file or just do some code to show system popup that shows options to view that file

huangapple
  • 本文由 发表于 2023年2月16日 14:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定