[英]Ionic3, displaying images from sd (or any) storage on android
I am working on an app which takes images from an external api and then saves them to the device for offline use later on, which is essential. 我正在开发一个应用程序,该应用程序从外部api获取图像,然后将其保存到设备中以供以后脱机使用,这是至关重要的。 This is currently done via the cordova-plugin-filetransfer .
目前,这是通过cordova-plugin-filetransfer完成的 。 They are stored in cordova.file.externalRootDirectory on my testing device now - I can see them and the files are okay.
它们现在存储在测试设备上的cordova.file.externalRootDirectory中 -我可以看到它们,并且文件还可以。
Now I want to display these Images in a page, but however I do it - nothing works. 现在,我想在页面中显示这些图像,但是不管怎么说,什么都没有用。 So before ending things for today I thought I'd ask.
所以在结束今天的事情之前,我想我会问。
<img src="{{director.data_dir}}{{project.picture_path}}" /><br />
<p>{{director.data_dir}}{{project.picture_path}}</p>
<img [attr.src]="director.display_path(project.picture_path)" /><br />
<p>{{director.display_dir}}{{project.picture_path}}</p>
None of them work. 他们都不工作。 I've tried several other ways, but I still get no image.
我尝试了其他几种方法,但是仍然没有图像。 The director.display_path() strips the file:// from the path as I've read I need to do this to get rid of this error:
正如我所读到的那样,director.display_path()从路径中剥离了file:// ,我需要这样做来消除此错误:
Not allowed to load local resource: file:///storage/emulated/0/
Okay guys, what am I doing wrong? 好的,我在做什么错? What should I do else?
我该怎么办?
I had the same problem, here it is how I solved. 我遇到了同样的问题,这就是我的解决方法。 You need DomSanitizer and File: Install ionic-native/file ( https://ionicframework.com/docs/native/file/ )
您需要DomSanitizer和文件:安装ionic-native / file( https://ionicframework.com/docs/native/file/ )
Assume you have 2 parameters: 假设您有2个参数:
mypath = "file:///emulated/0/Android/data/... .../yourfolder/" mypath =“文件:///仿真/ 0 / Android /数据/ ... ... /您的文件夹/”
myimage = "yourimage.jpg" myimage =“ yourimage.jpg”
In page.html file declare as: 在page.html文件中声明为:
<img [src]="cop">
Then in page.ts file: - Import DomSanitizer and File 然后在page.ts文件中:-导入DomSanitizer和文件
import {DomSanitizer} from '@angular/platform-browser'
import { File } from '@ionic-native/file';
Inject them in constructor(): 将它们注入constructor():
constructor(private file: File, private sanitizer: DomSanitizer) 构造函数(私有文件:File,私有消毒剂:DomSanitizer)
Set value of cop: 设定值:
getSanitizedImage(path, imageName){
this.file.readAsDataURL(path, imageName)
.then((data)=>{
let sanitized = this.sanitizer.bypassSecurityTrustUrl(data);
this.cop=sanitized;
})
.catch((err)=>{
console.log("error: " + JSON.stringify(err));
});
}
Now, just call getSanitizedImage(mypath, myimage), your variable "pos" will be updated and the <img>
object will display your image 现在,只需调用getSanitizedImage(mypath,myimage),变量“ pos”将更新,
<img>
对象将显示您的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.