我需要将图像路径保存到 sqlite 数据库。图像路径是 /images/filename.jpg,位于资源文件夹中。
我可以将所有其他数据保存到数据库中,而不是图像路径本身。我有一个页面,允许用户输入图像的目录详细信息,然后从图库中选择一张照片。
我只是不知道如何将图像文件路径保存到数据库,因此当用户导航回数据库记录时,他们会看到图像和信息。
目前只是信息保存而不是图像路径。
希望这是有道理的。
这是我到目前为止的页面代码:
var currentWin = Ti.UI.currentWindow;
//var id = currentWin.id;
function photoGalleryDidClose(item) {
photoView.image = item.media;
}
var db = Titanium.Database.open('photos');
var sql = db.execute('SELECT * FROM photos');
var recordID = sql.fieldByName('');
var title = sql.fieldByName('');
var location = sql.fieldByName('');
var photographer = sql.fieldByName('');
var equipment = sql.fieldByName('');
var notes = sql.fieldByName('');
var date = sql.fieldByName('');
var imageUrl = sql.fieldByName('');
//Labels to display catalogue details
var labelrecordID = Titanium.UI.createLabel({
text:'Record ID:',
font:{fontSize:18},
top:30,
left: 10,
height: 45,
width:'auto'
})
currentWin.add(labelrecordID);
var textrecordID = Titanium.UI.createTextField({
//text:' '+ title +'',
hintText: 'Enter an ID Number',
font:{fontSize:18},
color:'black',
borderRadius: 2,
top:30,
left: 150,
height: 45,
backgroundColor:'white',
width:'300'
})
textrecordID.addEventListener("change", function(e) {
newID = e.value;
});
currentWin.add(textrecordID);
var labelTitle = Titanium.UI.createLabel({
text:'Title:',
font:{fontSize:18},
top:80,
left: 10,
height: 45,
width:'auto'
})
currentWin.add(labelTitle);
var textTitle = Titanium.UI.createTextField({
text:' '+ title +'',
hintText: 'Title',
font:{fontSize:18},
color:'black',
borderRadius: 2,
top:80,
left: 150,
height: 45,
backgroundColor:'white',
width:'300'
})
textTitle.addEventListener("change", function(e) {
newTitle = e.value;
});
currentWin.add(textTitle);
///// I HAVE CHOPED OUT THE MIDDLE BIT AS IT IS JUST REPETITION OF ABOVE \\\\\
var labelDate = Titanium.UI.createLabel({
text:'Date:',
font:{fontSize:18},
top:330,
left: 10,
height: 45,
width:'auto'
})
currentWin.add(labelDate);
var textDate = Titanium.UI.createTextField({
text:' '+ date +'',
hintText: 'Date',
font:{fontSize:18},
color:'black',
borderRadius: 2,
top: 330,
left: 150,
height: 45,
backgroundColor:'white',
width:'300'
})
textDate.addEventListener("change", function(e) {
newDate = e.value;
});
currentWin.add(textDate);
// create a view to add the label and photo
var photoContainerView = Ti.UI.createView({
top: 400,
left: 10,
height: 230,
width: Ti.UI.FILL
});
var photoLabel = Ti.UI.createLabel({
text: 'Photo:',
left: 0,
height: Ti.UI.SIZE,
width: Ti.UI.SIZE,
font:{fontSize:18}
});
photoContainerView.add(photoLabel);
var photoView = Ti.UI.createImageView({
image: imageUrl,
top: 0,
left: 110,
height: 200,
width: 200,
borderColor: 'gray',
borderWidth: 1
});
photoContainerView.add(photoView);
var button_photo = Titanium.UI.createButton({
title: 'Add Image',
color: 'white',
backgroundColor: '#464646',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
top: 20,
right:10,
width: 130,
height: 60
});
button_photo.addEventListener('click', function(e){
//Titanium.Media.openPhotoGallery({allowEditing: true, success:photoGalleryDidClose});
Titanium.Media.openPhotoGallery({
success:function(event) {
var image = event.media;
photoView.image = image;
}
});
});
///////////PROBLEM HERE SAVING IMAGE INFO??\\\\\\\\\\
button_photo.addEventListener("click", function(e) {
newImage = e.value;
});
photoContainerView.add(button_photo);
var button_save = Titanium.UI.createButton({
title: 'Save Record',
color: 'white',
backgroundColor: '#464646',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
top: 100,
right:10,
width: 130,
height: 60,
//enabled:false
});
///Save the record - Add event listener for save button
button_save.addEventListener("click", function(e) {
//if (button_save.enabled) {
var db = Titanium.Database.open('photos');
db.execute('INSERT INTO photos (id,title,location,photographer,equipment,notes,date,imageUrl) VALUES(?,?,?,?,?,?,?,?)',newID,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,newImage);
var last = db.lastInsertRowId;
//}
});
photoContainerView.add(button_save);
currentWin.add(photoContainerView);
希望这是有道理的
正如我所说,除了图像路径之外,一切都很好。
我遇到的问题只是获取图像并为其分配正确的路径以将其保存在数据库中
感谢您的帮助
JC
更新更新更新
好吧,这是我所能得到的,我很困惑!我可以保存所有正确的细节和正确的图像路径(我认为),但它仍然不显示图像。这是选择然后保存图像的代码:
button_photo.addEventListener('click', function() {
Ti.Media.openPhotoGallery({
mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO],
success : function(e) {
photoView.image = e.media;
var time = new Date();
var name = '/images/name' + time.getTime() + '.jpg';
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, name);
f.write(e.media);
var newimage = f + '/' + name; //Just trying different things.
Ti.API.info('f.nativePath' + f.nativePath);
var db = Titanium.Database.open('photos');
db.execute('INSERT INTO photos (id,title,location,photographer,equipment,notes,date,imageUrl) VALUES(?,?,?,?,?,?,?,?)', name,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,name);
db.close();
//insertdb(newID,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,f);
//aTableView.setData(getData());
},
cancel : function() {
},
error : function(err) {
}
});
});
我是否将图像路径保存到“images/filname.png”或“f.nativePath”似乎没有任何区别?
我在数据库中有一些预先填充的字段,图像存储在“images/filname.jpg”中,这些显示在我的应用程序中很好。只是当我尝试从手机图库中添加图片时,它没有显示出来......
任何帮助都会很棒!
谢谢JC