javascript ImageView 使用存储在 Titanium.Filesystem.applicationDataDirectory 中的图像显示占位符,而不是图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5730983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImageView using image stored at Titanium.Filesystem.applicationDataDirectory displays placeholder, not image
提问by joseym
I'm developing with SDK 1.6.2.
我正在使用 SDK 1.6.2 进行开发。
My app uses the camera to capture and save an image to Titanium.Filesystem.applicationDataDirectory.
我的应用程序使用相机捕获图像并将其保存到 Titanium.Filesystem.applicationDataDirectory。
A tap of the app is supposed to display all stored images (details [path] stored in database) tiled across the screen.
轻按一下该应用程序应该会在屏幕上平铺显示所有存储的图像(存储在数据库中的详细信息 [路径])。
Saving the image:
保存图像:
var image = event.media // from camera success
var filename = new Date().getTime() + "-ea.jpg";
bgImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
bgImage.write(image);
Storing to database:
存储到数据库:
var db = Titanium.Database.open('photoDB');
try{
db.execute('INSERT INTO stored (image) VALUES (?)', bgImage.nativePath);
} catch(e) {
alert(e.message);
}
db.close();
Showing the Images:
显示图像:
imageArray = [];
images = [];
var db = Titanium.Database.open('photoDB');
var dbrows = db.execute('select id, date, image from stored order by date asc');
while (dbrows.isValidRow()) {
imageArray.push({
image:dbrows.fieldByName('image')
});
dbrows.next();
}
dbrows.close();
// loop thru and display images
for (var i = 0; i < imageArray.length; i++){
var pushleft = (i % 4) * 75; // tile from left
var pushtop = Math.floor(i/4) * 96; // determine how far from top
var file = Titanium.Filesystem.getFile(imageArray[i].image);
images[i] = Ti.UI.createImageView({
image: imageArray[i].image, // path to image at applicationDataDirectory
width: 75,
height: 96,
left: pushleft + 5, // logic for positioning
top: pushtop + 5, // logic for positioning
store_id: imageArray[i].id
});
win.add(images[i]);
}
Unfortunately, while the tiles work the images are just showing the image placeholder, not the stored image.
不幸的是,虽然瓷砖工作,但图像只是显示图像占位符,而不是存储的图像。
I have phonedisk, so after building the app for my device I can view the application directory and the images are being stored.
我有 phonedisk,所以在为我的设备构建应用程序后,我可以查看应用程序目录并存储图像。
What am I missing?
我错过了什么?
回答by joseym
Figured it out, thanks everyone for the help ;) < sarcasm (It's only been a day, I hold no grudges)
想通了,谢谢大家的帮助;) < 讽刺(才一天而已,我没有怨恨)
Here's what was wrong in case anybody else has a similar issue.
如果其他人有类似的问题,这里是错误的。
// Create a file name
var filename = new Date().getTime() + "-ea.jpg";
// Create the file in the application directory
bgImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
// Write the image to the new file (image created from camera)
bgImage.write(image);
When I was storing the image location in the database I was storing the full path bgImage.nativePath
. However, when I updated and rebuild the app the apps applicationDataDirectorychanged, so the stored path was invalid.
当我在数据库中存储图像位置时,我存储的是完整路径bgImage.nativePath
。但是,当我更新和重建应用程序时,应用程序 applicationDataDirectory发生了变化,因此存储的路径无效。
So now I just store var filename
in the database and when I display it like this:
所以现在我只是存储var filename
在数据库中,当我像这样显示它时:
images[i] = Ti.UI.createImageView({
image: Titanium.Filesystem.applicationDataDirectory + Ti.Filesystem.separator + imageArray[i].image, // path to image at applicationDataDirectory
width: 75,
height: 96,
left: pushleft + 5, // logic for positioning
top: pushtop + 5, // logic for positioning
store_id: imageArray[i].id
});
Now, even with updates, it always points to the proper applicationDataDirectory
现在,即使有更新,它也总是指向正确的 applicationDataDirectory