ionic cordova:当我从 filechooser 插件获取 content:// url 时,如何在 android gallery 的 img 标签中显示图像

2024-02-14

我浪费了 6 天(并且还在增加)努力使用 ionic3-cordova 在 Android 上显示图库中的图像

this.fileChooser.open()
  .then((uri) => {
    console.log('image uri is', uri);

  })

我不能使用uri直接绑定到我的标签的src

我尝试使用另一个插件并获取文件条目

return this.file.resolveLocalFilesystemUrl(uri);    

我也不能使用它...不安全的uri!

然后我用了this.domSanitizer.bypassSecurityTrustUrl(entry.toInternalURL());

我不能用它来绑定到 img src

我不明白获取 uri、转换为另一个以确保安全,然后再次转换为另一个以提高安全性的意义...但仍然无法达到目的...

除了挫折之外,请帮助我如何在这个不断变化的角度科尔多瓦离子世界中将html img标签的src绑定到从图库中选择的图像..一个学期前回答的文档/问题已经过时了角度6

我在index.html 的内容安全策略标记中添加了 cdvfile:

我像这样在 html 中绑定 img 标签

<img class="profile-img" [src]="imageSrc" />

尿布更改条目的内部 url 并将其绑定到 img src 后得到的最终错误是

GET cdvfile://localhost/content/com.android.providers.media.documents/document/image%3A4350:1 GET cdvfile://localhost/content/com.android.providers.media.documents/document/image%3A4350 0 ()
Image (async)
DefaultDomRenderer2.setProperty @ platform-browser.js:1283
DebugRenderer2.setProperty @ core.js:12365
setElementProperty @ core.js:9112
checkAndUpdateElementValue @ core.js:9063
checkAndUpdateElementInline @ core.js:9010
checkAndUpdateNodeInline @ core.js:11359
checkAndUpdateNode @ core.js:11325
debugCheckAndUpdateNode @ core.js:11962
debugCheckRenderNodeFn @ core.js:11948
(anonymous) @ AddSalonPage.html:32
debugUpdateRenderer @ core.js:11940
checkAndUpdateView @ core.js:11312
callViewAction @ core.js:11548
execComponentViewsAction @ core.js:11490
checkAndUpdateView @ core.js:11313
callViewAction @ core.js:11548
execEmbeddedViewsAction @ core.js:11511
checkAndUpdateView @ core.js:11308
callViewAction @ core.js:11548
execComponentViewsAction @ core.js:11490
checkAndUpdateView @ core.js:11313
callViewAction @ core.js:11548
execComponentViewsAction @ core.js:11490
checkAndUpdateView @ core.js:11313
callViewAction @ core.js:11548
execEmbeddedViewsAction @ core.js:11511
checkAndUpdateView @ core.js:11308
callViewAction @ core.js:11548
execComponentViewsAction @ core.js:11490
checkAndUpdateView @ core.js:11313
callWithDebugContext @ core.js:12204
debugCheckAndUpdateView @ core.js:11882
ViewRef_.detectChanges @ core.js:9692
(anonymous) @ core.js:5086
ApplicationRef.tick @ core.js:5086
(anonymous) @ core.js:4929
t.invoke @ polyfills.js:3
onInvoke @ core.js:4062
t.invoke @ polyfills.js:3
r.run @ polyfills.js:3
NgZone.run @ core.js:3918
next @ core.js:4929
schedulerFn @ core.js:3712
SafeSubscriber.__tryOrUnsub @ Subscriber.js:253
SafeSubscriber.next @ Subscriber.js:191
Subscriber._next @ Subscriber.js:129
Subscriber.next @ Subscriber.js:93
Subject.next @ Subject.js:53
EventEmitter.emit @ core.js:3704
checkStable @ core.js:4031
onHasTask @ core.js:4075
t.hasTask @ polyfills.js:3
t._updateTaskCount @ polyfills.js:3
r._updateTaskCount @ polyfills.js:3
r.runTask @ polyfills.js:3
o @ polyfills.js:3
Promise.then (async)
r @ polyfills.js:3
t.scheduleTask @ polyfills.js:3
onScheduleTask @ polyfills.js:3
t.scheduleTask @ polyfills.js:3
r.scheduleTask @ polyfills.js:3
r.scheduleMicroTask @ polyfills.js:3
f @ polyfills.js:3
c @ polyfills.js:3
(anonymous) @ polyfills.js:3
(anonymous) @ index.js:530
(anonymous) @ resolveLocalFileSystemURI.js:76
success @ fileSystems-roots.js:37
callbackFromNative @ cordova.js:291
(anonymous) @ VM699:1

我的离子离子信息是

cli packages: (---\AppData\Roaming\npm\node_modules)

    @ionic/cli-utils  : 1.19.2
    ionic (Ionic CLI) : 3.20.0

global packages:

    cordova (Cordova CLI) : 8.0.0

local packages:

    @ionic/app-scripts : 3.1.9
    Cordova Platforms  : none
    Ionic Framework    : ionic-angular 3.9.2

System:

    Android SDK Tools : 26.1.1
    Node              : v9.2.0
    npm               : 5.8.0
    OS                : Windows 7

我的 package.json 内容是

"dependencies": {
    "@angular/animations": "6.0.1",
    "@angular/cli": "^6.0.1",
    "@angular/common": "6.0.1",
    "@angular/compiler": "6.0.1",
    "@angular/compiler-cli": "^6.0.1",
    "@angular/core": "6.0.1",
    "@angular/forms": "6.0.1",
    "@angular/http": "6.0.1",
    "@angular/platform-browser": "6.0.1",
    "@angular/platform-browser-dynamic": "6.0.1",
    "@ionic-native/camera": "^4.7.0",
    "@ionic-native/core": "^4.7.0",
    "@ionic-native/date-picker": "^4.7.0",
    "@ionic-native/file": "^4.7.0",
    "@ionic-native/file-chooser": "^4.7.0",
    "@ionic-native/geolocation": "^4.7.0",
    "@ionic-native/google-maps": "^4.8.2",
    "@ionic-native/network": "^4.7.0",
    "@ionic-native/splash-screen": "^4.7.0",
    "@ionic-native/status-bar": "^4.7.0",
    "@ionic-native/streaming-media": "^4.7.0",
    "@ionic-native/transfer": "^3.14.0",
    "@ionic-native/video-editor": "^4.7.0",
    "@ionic/storage": "^2.1.3",
    "@types/google-maps": "^3.2.0",
    "cordova-android": "7.1.0",
    "cordova-plugin-camera": "^4.0.3",
    "cordova-plugin-datepicker": "^0.9.3",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-filechooser": "^1.0.1",
    "cordova-plugin-geolocation": "^4.0.1",
    "cordova-plugin-googlemaps": "^2.3.1",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.19",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-streaming-media": "^1.0.2",
    "cordova-plugin-video-editor": "^1.1.3",
    "cordova-plugin-whitelist": "^1.3.3",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "rxjs": "^6.0.0",
    "rxjs-compat": "^6.1.0",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  }

任何帮助是极大的赞赏


我让它与 ImagePicker 一起工作。我是这样做的

在 TS 文件中:

import { File, FileEntry } from '@ionic-native/file';
import { DomSanitizer } from '@angular/platform-browser';
import { ImagePicker } from '@ionic-native/image-picker';  

...

    constructor(
        ...
        private file: File,
        private domSanitizer: DomSanitizer,
        private imagePicker: ImagePicker
    ) { }

    ...

    getImageFromGallery() {
        return this.imagePicker.getPictures({
            maximumImagesCount: 1
        })
        .then((results) => {
            return this.file.resolveLocalFilesystemUrl(results[0]);
        })
        .then((entry0: FileEntry) => {
            this.imageSrc = this.domSanitizer.bypassSecurityTrustUrl(entry0.toInternalURL());
        })
        .catch((err) => {
            console.log('failed to get image', err);
        });
    }

HTML 是<img [src]="imageSrc" class="profile-image" />

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ionic cordova:当我从 filechooser 插件获取 content:// url 时,如何在 android gallery 的 img 标签中显示图像 的相关文章

随机推荐