Accessing Images in PhotoGallery
Hello Community,
How do I save an image location that I have included in the the photogallery and display the image the next time the app is opened. Here is the scenario. I take a picture, save it to the photogallery and display it on the screen. I then close the app and when I come back to the app I want to be able to display the same picture from the photogallery. I don't see how to save the index to to the image for return use. Thanks for any and all help.
5 Answers
-
Take Picture:
Titanium.Media.showCamera({ success:function(event) { var cropRect = event.cropRect; var image = event.media; var thumbnail = event.thumbnail; var filename = "camera.png"; Titanium.Api.Properties.setString("filename", filename); var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); f.write(image); win.backgroundImage = f.nativePath; }, cancel:function() { }, error:function(error) { // create alert var a = Titanium.UI.createAlertDialog({title:'Camera'}); // set message if (error.code == Titanium.Media.NO_CAMERA) { a.setMessage('Device does not have video recording capabilities'); } else { a.setMessage('Unexpected error: ' + error.code); } // show alert a.show(); }, allowImageEditing:true, });
Then when you reload your application you can check if the file is there.
var image = Ti.Ui.createImageView({ height: "auto", width: "auto" }); if (Ti.Api.Properties.getString("filename") != null) { // we have the file, so show it var filename = Ti.Api.Properties.getString("filename"); var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); image.url = f; }
I didn't run this, so the syntax may be off a bit.
-
Rob your code was close to working. There were a couple of case issues on API and UI and a couple of spots where API should have been App so I changed those and it worked beautifully.
Titanium.Media.showCamera({ success:function(event) { var cropRect = event.cropRect; var image = event.media; var thumbnail = event.thumbnail; var filename = "camera.png"; Titanium.App.Properties.setString("filename", filename); var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); f.write(image); win.backgroundImage = f.nativePath; }, cancel:function() { }, error:function(error) { // create alert var a = Titanium.UI.createAlertDialog({title:'Camera'}); // set message if (error.code == Titanium.Media.NO_CAMERA) { a.setMessage('Device does not have video recording capabilities'); } else { a.setMessage('Unexpected error: ' + error.code); } // show alert a.show(); }, allowImageEditing:true }); var image = Titanium.UI.createImageView({ height: "auto", width: "auto" }); if (Titanium.App.Properties.getString("filename") != null) { // we have the file, so show it var filename = Titanium.App.Properties.getString("filename"); var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); image.url = f; } win.add(image);
-
I don't think the Url or Path to an image in the gallery is available in 1.0.
I do something similar by doing the following:
1) Save the image as a blob to the applicationDataDirectory
2) Save a property letting me know the file name, and any other info
3) Then save the image to the photogalleryWhen I load my app, I check that property to see if the user has selected an image and load the image from the applicationDataDirectory
Although more work then just referencing the photogallery it does allow you to keep the image even if the user removes it from their gallery.
-
Thanks Ben for the help. I was afraid that was the case. I just did not want to keep two copies of the same image on the phone but I think I will need to go with your solution.
-
Hi all, just getting started with Ti. Is it still the case that you cannot refer to the filesystem path of a photo in the photo gallery? It seems ridiculous to store a duplicate copy of the photo in the applicationDataDirectory. Anyone know if the Appcelerator folks have fixed this?