![](/img/trans.png)
[英]Error: ENOENT: no such file or directory when running Ionic Cordova Platform Add Android
[英]Ionic 3 after running ionic cordova add platform android, error occurs to my ionic-native/file,filepath,transfer
我有類似問題的錯誤,當我嘗試設計我的應用程序以調用 native.camera 時,我在 ionic 3 項目中看到了我的控制台,我看到了這個錯誤:
本機:嘗試調用 Camera.getPicture,但 Cordova 不可用。 確保包含cordova.js 或在設備/模擬器中運行。
這是我過去稱為原生相機的代碼。
這是我的 issue.html 中的代碼
<button class="logoCamera" ion-button (click)="presentActionSheet()">
<ion-icon name="camera" ></ion-icon>
這是我的問題.ts中的代碼
import { File } from '@ionic-native/file';
import { Transfer, TransferObject} from '@ionic-native/transfer';
import { FilePath } from '@ionic-native/file-path';
import { Camera } from '@ionic-native/camera';
public presentActionSheet(){
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image',
buttons: [
{
text: 'Load from Library',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
public takePicture(sourceType){
//Create option for the Camera dialog
var options = {
quality: 100,
sourceType : sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
//Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
//special handling for android lib
if(this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1 );
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/')+ 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast('Error while selecting Image.');
});
}
//Create a new name for image
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
return newFileName;
}
//copy image to local folder
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast('Error while storing file.');
});
}
private presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 3000,
position: 'middle'
});
toast.present();
}
public pathForImage(img){
if (img === null) {
return '';
} else {
return cordova.file.dataDirectory + img;
}
}
public uploadImage() {
//destination URL
var url = "";
//file to upload
var targetPath = this.pathForImage(this.lastImage);
//file name only
var filename = this.lastImage;
var options = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params: {'fileName': filename}
};
const fileTransfer: TransferObject = this.transfer.create();
this.loading = this.loadingCtrl.create({
content: 'Uploading...',
});
this.loading.present();
//use FileTransfer to upload image
fileTransfer.upload(targetPath, url, options).then(data => {
this.loading.dismissAll()
this.presentToast('Image successful uploaded.');
}, err => {
this.loading.dismissAll()
this.presentToast('Error while uploading file.');
});
}
當我運行 ionic serve 時,一切都很順利,沒有錯誤,什么也沒有。
但是當我點擊我的按鈕訪問natve相機時,錯誤顯示,請幫我找出問題,我查了很多網頁,都沒有解決我的問題。
在我嘗試運行 ionic cordova run ios --simulator 后,出現錯誤,但我很確定在運行此命令之前此錯誤不存在。
我可以知道如何解決這個問題嗎??
錯誤消息在這里非常准確:
本機:嘗試調用 Camera.getPicture,但 Cordova 不可用。 確保包含cordova.js或在設備/模擬器中運行。
運行ionic serve
不包含cordova.js,也不在模擬器或設備上運行您的應用程序,這就是您收到錯誤的原因。 您可以通過在設備或模擬器上運行您的應用程序來修復它:
ionic cordova run android/ios --device/--simulator
或者通過添加瀏覽器平台:
cordova platform add browser
並運行瀏覽器平台:
ionic cordova run browser
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.