gpt4 book ai didi

android - ionic 文件下载不起作用

转载 作者:行者123 更新时间:2023-11-29 15:00:35 24 4
gpt4 key购买 nike

我正在构建一个壁纸 ionic 应用程序。

在应用程序中,显示了存储在 www/assets/img 中的图像。我在下面构建了 2 个按钮,用于将显示的图像下载和检索到移动设备内存。

当我单击下载按钮时,会显示一个对话框,显示“下载成功!Pug.jpg 已成功下载到:文件路径”。但是当我检查手机内存时,没有这样的文件。此外,当我单击“检索”时按钮显示对话框显示“文件检索成功!Pug.jpg 已成功检索自:文件路径”,即使手机内存中不存在该文件。

这是 home.ts 代码

import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';

declare var cordova: any;

@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [Transfer, TransferObject, File]
})

export class HomePage {

storageDirectory: string = '';

constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}

if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.dataDirectory;
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}

downloadImage(image) {

this.platform.ready().then(() => {

const fileTransfer: TransferObject = this.transfer.create();

const imageLocation = `${cordova.file.applicationDirectory}www/assets/img/${image}`;

fileTransfer.download(imageLocation, this.storageDirectory + image).then((entry) => {

const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `${image} was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});

alertSuccess.present();

}, (error) => {

const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `${image} was not successfully downloaded. Error code: ${error.code}`,
buttons: ['Ok']
});

alertFailure.present();

});

});

}

retrieveImage(image) {

this.file.checkFile(this.storageDirectory, image)
.then(() => {

const alertSuccess = this.alertCtrl.create({
title: `File retrieval Succeeded!`,
subTitle: `${image} was successfully retrieved from: ${this.storageDirectory}`,
buttons: ['Ok']
});

return alertSuccess.present();

})
.catch((err) => {

const alertFailure = this.alertCtrl.create({
title: `File retrieval Failed!`,
subTitle: `${image} was not successfully retrieved. Error Code: ${err.code}`,
buttons: ['Ok']
});

return alertFailure.present();

});
}

}

这是 home.html 代码

<ion-header>
<ion-navbar>
<ion-title>
File Transfer Example
</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<ion-card>
<ion-card-header>
Ionic 3 File Transfer Example
</ion-card-header>
<ion-card-content>
<img src="assets/img/pug.jpg" alt="Cute Pug">
<button ion-button (click)="downloadImage('pug.jpg')" color="secondary">Download image</button>
<button ion-button (click)="retrieveImage('pug.jpg')" color="secondary">Retrieve downloaded image</button>
</ion-card-content>
</ion-card>
</ion-content>

我基于这个 Github code example 构建了这个 ionic 应用程序

我实际上希望 ionic 应用程序首先在内存中创建一个文件夹(应用程序命名的文件夹)并将所有图像放在那里。这样用户就可以访问该文件夹中的文件。例如,如果应用程序名称是“Appsample”,则所有图像应位于内存中的 Appsample 文件夹中。

我如何才能实现上述目的?

谢谢。

最佳答案

我刚刚发布了几乎相同问题的答案,请参阅:

Download not working using filetransfer plugin .

这里的主要问题是您使用以下目录来保存文件:

 else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.dataDirectory;
}

如 cordova 文档 ( https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#where-to-store-files ) 中所述,“cordova.file.dataDirectory”是使用内部内存的应用程序沙箱中的持久且私有(private)的数据存储。

使用cordova.file.externalDataDirectory来满足您的目的。然后该文件应放置在此处的某个位置:“file:///storage/emulated/0/Android/data/subdomain.domainname.toplevdomain/files/...”。

在 Android 上,外部存储目录始终存在。如果设备没有物理卡,Android 将模拟它。

关于android - ionic 文件下载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com