Android 将图像路径保存到sqlite数据库

Android 将图像路径保存到sqlite数据库,android,database,image,sqlite,Android,Database,Image,Sqlite,我需要将图像路径保存到sqlite数据库。图像路径应为/images/filename.jpg,位于resources文件夹中 我可以将所有其他数据保存到数据库,而不是图像路径本身。 我有一个页面,允许用户输入图像的目录详细信息,并从图库中选择照片 我只是不知道如何将图像文件路径保存到数据库,这样当用户导航回数据库记录时,他们可以看到图像和信息 目前只保存信息,不保存图像路径 希望这是有意义的 以下是我到目前为止的页面代码: var currentWin = Ti.UI.currentWindo

我需要将图像路径保存到sqlite数据库。图像路径应为/images/filename.jpg,位于resources文件夹中

我可以将所有其他数据保存到数据库,而不是图像路径本身。 我有一个页面,允许用户输入图像的目录详细信息,并从图库中选择照片

我只是不知道如何将图像文件路径保存到数据库,这样当用户导航回数据库记录时,他们可以看到图像和信息

目前只保存信息,不保存图像路径

希望这是有意义的

以下是我到目前为止的页面代码:

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

我假设我需要这样的东西:
//保存以备将来使用var f=Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'photo'+currentWin.id+'.png');f.书写(图像);db.addPhoto(currentWin.id,f.nativePath)这是来自Tianium网站的示例,但我不知道如何在我的案例中实际实现它?感谢您下面的代码列表似乎使用了
var name
变量来填充数据库的
id
imageURL
字段(在
db.execute
命令中)。我错了吗?或者这是故意的吗?你好,谢谢你的评论。我故意这样做是为了查看保存的文件路径,以了解发生了什么。我也有同样的问题。你能显示图像吗?我花了一天的时间在这上面,但在将本机路径存储到db之后,我不知道如何显示图像。