使用存储在 Titanium.Filesystem.applicationDataDirectory 中的图像的 ImageView 显示占位符,而不是图像

发布于 2024-11-02 05:33:52 字数 1741 浏览 0 评论 0原文

我正在使用 SDK 1.6.2 进行开发。

我的应用程序使用相机捕获图像并将其保存到 Titanium.Filesystem.applicationDataDirectory。

点击应用程序应该会在屏幕上平铺显示所有存储的图像(存储在数据库中的详细信息[路径])。

保存图像:

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);

存储到数据库:

var db = Titanium.Database.open('photoDB');
try{
    db.execute('INSERT INTO stored (image) VALUES (?)', bgImage.nativePath);
} catch(e) {
    alert(e.message);
}

db.close();

显示图像:

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]);
}

不幸的是,虽然图块工作,但图像仅显示图像占位符,而不是存储的图像。

我有phonedisk,因此在为我的设备构建应用程序后,我可以查看应用程序目录和正在存储的图像。

我缺少什么?

I'm developing with SDK 1.6.2.

My app uses the camera to capture and save an image to 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.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-11-09 05:33:52

搞清楚了,谢谢大家的帮助;) <讽刺(才一天,我没有怨恨)

如果其他人有类似的问题,这就是错误所在。

// 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);

当我将图像位置存储在数据库中时,我存储了完整路径bgImage.nativePath。但是,当我更新并重建应用程序时,应用程序 applicationDataDirectory 发生了更改,因此存储的路径无效。

所以现在我只是将 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
});

现在,即使有更新,它也始终指向正确的 applicationDataDirectory

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 applicationDataDirectory changed, so the stored path was invalid.

So now I just store var filename in the database and when I display it like this:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文