Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Save image to phone
Started by jn19 Sep 27 2011 03:16 AM

- - - - -
46 replies to this topic
[TOPIC CONTROLS]
Page 2 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#26

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

vovasoft,

I'm not sure what you mean by "default phone image viewer". Do you mean Android's standard "Gallery" app? If it is displaying a black image, well then that is a bug on Google's end, not ours. The photo that you are saving to your Documents directory is just a "file copy" of the image file in the Gallery app.

Now, if the image that is in your Documents directory is solid black, then that's a different story. If that is the issue, then what happens if you display that image via
display.newImageRect()
within Corona? Does it display correctly? It has for all of my tests. I've also successfully e-mail that saved photos via our
native.showPopup()
API as well.
uid: 32256 topic_id: 15661 reply_id: 115825


[TOPIC: post.html]
#27

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

Joshua Quick,
1. Yes I mean Android standard "Gallery" app. I tried other 4 different image viewer but all said operation failed and my file browser shows me "operation failed" in MD5 description of image but the file size is the same of the photo from Gallery..
2. It's ok when I open the image via display.newImageRect() within Corona but how to get width and height of images ? I cannot create an image without width and height.
3. Then I e-mail that saved photo via native.showPopup()
local options =	{		to = "user@name.com",		subject = "Image",		body = "some text",		attachment = { baseDir=system.DocumentsDirectory,		filename="imageName.jpg", type="image" },	}	native.showPopup("mail", options)

I receive an error "cannot attach the file" with android default e-mail client.
But it works ok with gmail e-mail app from appstore but the default browser is more important
My android version is: 2.3.6
What do you think ?
uid: 138389 topic_id: 15661 reply_id: 115847


[TOPIC: post.html]
#28

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

So, I assume you are trying to do use the
media.save()
function, correct? Is that what is failing on you? Because your sample code up above is using
media.show()
. That's why I'm a bit confused here.
uid: 32256 topic_id: 15661 reply_id: 115849


[TOPIC: post.html]
#29

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

No. I'm trying to copy a photo from Photo Gallery using
media.show()
.
local onComplete = function(event)   -- do nothingend file ={        baseDir = system.DocumentsDirectory,        filename = "imageName.jpg",        type = "image"} media.show( media.PhotoLibrary, onComplete, file )

I don't want to display it, just to save the chosen image to a file.and store the filename in json file, I will use the image in the future.
It works ok but
When I load the saved image using
display.newImageRect( [parentGroup,] filename [, baseDirectory] width, height )

I don't know the original width and height of that photo. What can I do ?
uid: 138389 topic_id: 15661 reply_id: 115860


[TOPIC: post.html]
#30

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Oh, yeah, Corona does not provide an API for fetch width/height from an image file. You have to use
display.newImage()
in this case since you do not know the width and height of the image. That function will automatically downscale the image "proportionally" to fit within OpenGL's constraints.

You can scale it lower from there to fit within the screen's bounds if needed. You should use the display object's
scale()
function to downscale. Never adjust an image object's width and height. Those 2 properties indicate the image's width and height in pixels and they do NOT change when scaling the object. The
contentWidth
and
contentHeight
properties are the width and height of the object onscreen and these do change when scaling the object.

Now, if the image fails to load, then it sounds like the save-to-file operation failed. Perhaps because you are out of storage space. Have a look at your Android's log output for clues, which you can view via "adb logcat" or "DDMS".
uid: 32256 topic_id: 15661 reply_id: 115864


[TOPIC: post.html]
#31

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

Joshua Quick , You helped me a lot. Thank you.

That function will automatically downscale the image "proportionally" to fit within OpenGL's constraints.
What are this OpenGL's constraints ?
uid: 138389 topic_id: 15661 reply_id: 115920


[TOPIC: post.html]
#32

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

We use OpenGL (Open Graphics Library) to render content with hardware acceleration. The graphics hardware has a limit to how large an image/texture it can hold in memory. This is referred to as the "max texture size" (aka: GL_MAX_TEXTURE_SIZE), which is the maximum width/height of a texture it can hold in pixels. The maximum varies between different graphics hardware, but on average, I find most mobile devices support up to 2048 pixels.

On the old Droid, the max is 1024 pixels. (Pathetically small.)
I find most Android devices support a max texture size of 2048 pixels.

This max texture size usually comes into play with photos because they are usually larger than what OpenGL can display. In order to display a large photo selected from the gallery, Corona has no choice but to downscale the image in memory before feeding it to OpenGL in order to render it. This is really done via downsampling, which means we skip every other pixel in the image file when loading it into memory. There is really no other way to do it. But that said, the image will still be displayed within your app and it will still appear super huge. So, likely you will never notice this is happening unless you look at your Android device's log output which will indicate when downsampling has occurred. If your device does not have enough memory to load the image, then the log output will indicate this as well. That's more likely to happen on older device with low RAM and if that case occurs, Corona will attempt to load the image one more time by downsampling it further, but if that final attempt fails, then we have no choice but to give up.
uid: 32256 topic_id: 15661 reply_id: 115926


[TOPIC: post.html]
#33

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

1. Corona will attempt to load the image one more time by downsampling it further, but if that final attempt fails, then we have no choice but to give up.
The code will stop running or it will continue to run the code ?

2. What about the error I mentioned above ?
I receive an error "cannot attach the file" with android default e-mail client.
But it works ok with gmail e-mail app from appstore but the default browser is more important
My android version is: 2.3.6
What do you think ?
uid: 138389 topic_id: 15661 reply_id: 115928


[TOPIC: post.html]
#34

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

vovasoft,

If Corona fails to load the image, then its associated function such as
display.newImage()
is "supposed" to return nil. Either that or a blank polygon. Unfortunately, I personally don't remember which it does... but it definitely does not crash.

Regarding the error, I'm not sure what to think. No one else has mentioned a similar issue and we can't reproduce it, so you'll need to isolate it. I suggest that you look at the Android device log output via "adb logcat" or "ddms" to get more details. For example, it might be that the photo file is too large for the e-mail app to send and that's why it's failing... but only the log can provide that kind of detail.
uid: 32256 topic_id: 15661 reply_id: 115935


[TOPIC: post.html]
#35

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

The only error I found with logcat is
[java][ 07-18 22:50:50.832 1741:0x8a1 E/UsbStorageConnector ]

Error in NativeDaemonConnector

java.io.IOException: No such file or directory

at android.net.LocalSocketImpl.connectLocal(Native Method)

at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:238)

at android.net.LocalSocket.connect(LocalSocket.java:98)

at com.android.server.NativeDaemonConnector.listenToSocket(NativeDaemonConnector.java:102)

at com.android.server.NativeDaemonConnector.run(NativeDaemonConnector.java:85)

at java.lang.Thread.run(Thread.java:1019)[/java]
It is fired up every 5 seconds
uid: 138389 topic_id: 15661 reply_id: 115945


[TOPIC: post.html]
#36

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

You said it works with the "gmail e-mail app", but not with the browser. I don't really understand what you are doing. If you are e-mailing the file via the
native.showPopup("mail")
and it works, then why not use that method? If you are trying to e-mail the file via a web popup then that is something we've never tried before and it's up to you to figure that out. We've gone well out of our way on our end to make Corona support e-mail files via our
native.showPopup()
function, it's well tested, and it works... so I suggest you use it.
uid: 32256 topic_id: 15661 reply_id: 115947


[TOPIC: post.html]
#37

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

Not browser but file browser. I said it works with the "gmail e-mail app", but not with "default android email app" that is used by most people.
I know that
native.showPopup("mail")
works with files, but when i try to e-mail an image that previously was saved using this code
local onComplete = function(event)   -- do nothingend file ={        baseDir = system.DocumentsDirectory,        filename = "imageName.jpg",        type = "image"} media.show( media.PhotoLibrary, onComplete, file )

the image cannot be send via default android email app.
uid: 138389 topic_id: 15661 reply_id: 115949


[TOPIC: post.html]
#38

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Okay, we're not getting anywhere on this forum thread. :)
I know for a fact that this works, but there must be a bug in your code. Like your app is trying to e-mail the file before your onComplete() event has been triggered... thus the file doesn't exist yet.

How about you send us a bug report with a small zipped up sample project that can reproduce this issue. You can do so by clicking the "Report A Bug" link at the top of this web page.
uid: 32256 topic_id: 15661 reply_id: 116093


[TOPIC: post.html]
#39

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

Ok. I will send the report like you said.
One more question: Is there a way to save/backup a file to sdcard or external sdcard with LuaFileSystem ?
uid: 138389 topic_id: 15661 reply_id: 116296


[TOPIC: post.html]
#40

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Regarding writing to the SD card, there isn't a reliable way to do this right now. The problem is that you don't want to hard code the path to the SD card because there isn't a standard path between different Android device manufacturers. The proper way to do this is to fetch the path to "external storage" from the Android OS and use that path. We would have to provide an API in Corona in order to do this.
uid: 32256 topic_id: 15661 reply_id: 116747


[TOPIC: post.html]
#41

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

@Joshua Quick , please help me. I have a big problem.
I need to save an image to file from the sceen. I use
display.save()


display.contentWidth is 480
display.contentHeight is 800
local image = display.newImageRect( "my_image.jpg", 480, 800 )image.x = display.contentCenterXimage.y = display.contentCenterYlocal group = display.newGroup()group:insert(image)display.save( group, "image1.jpg", system.DocumentsDirectory )

But it does not save a 480x800 image. The size of the saved image is 240x400.
I deleted the config.lua, so nothing content scaling.
Please tell me how to save a full size image.
uid: 138389 topic_id: 15661 reply_id: 117740


[TOPIC: post.html]
#42

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

The saved image is going to be in pixels, not in content coordinates. That means the capture image will vary in size on screens with different resolutions. This is by design.

I recommend that you display the capture image via
display.newImageRect()
and feed that function the width and height that you want the image object to have onscreen. This way Corona will automatically scale the capture image for you when displaying it.

Also... why are you capturing that temporary group object that only has 1 image object in it? That seems unnecessary. There's no point in capturing a group unless you want to capture a collection of objects in one shot. Just give the
display.capture()
function the one image object that you want to capture directly. Much easier and less trouble.
uid: 32256 topic_id: 15661 reply_id: 117784


[TOPIC: post.html]
#43

vovasoft

[GLOBAL: userInfoPane.html]
vovasoft
  • Contributor

  • 131 posts
  • Corona SDK

@Joshua Quick, [text] The saved image is going to be in pixels, not in content coordinates. [/text]
I don't understand this yet thing but I displayed the save image like you said and it work perfect.
I changed the code
local image = display.newImageRect( "image.jpg", 480, 800)
image.x = display.contentCenterX
image.y = display.contentCenterY
display.save( image, "image1.jpg", system.DocumentsDirectory )
You said I can use
display.capture()
But it saves the images to "photo library" and not to "system.DocumentsDirectory".
I need this to make and save Thumbnails of very big photos.
You helped me a lot. I was blocked here. Many thanks.
uid: 138389 topic_id: 15661 reply_id: 117793


[TOPIC: post.html]
#44

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

My mistake. I meant
display.save()
. Sorry to confuse you.

The code you are showing up above is perfect! Glad I could help.
uid: 32256 topic_id: 15661 reply_id: 117795


[TOPIC: post.html]
#45

ali.ayyaz

[GLOBAL: userInfoPane.html]
ali.ayyaz
  • Enthusiast

  • 60 posts
  • Corona SDK

Joshua,
I was going through this post and I require help regarding something similar so I thought of posting in this thread instead of making a new one.

I'm taking pictures with the camera and saving them using display.save. The images get saved in the documentsDirectory alright, but I can't seem to locate them when I run the app on my IPad. Where are these images saved?

Secondly, I tried using display.capture. It saves the images on my desktop but names them as picture1, picture2 and so on. While I want to save the names of pictures as date and time stamp. How can i do that?
Thanks.
uid: 175611 topic_id: 15661 reply_id: 136336


[TOPIC: post.html]
#46

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

iOS sandboxes your app's directories (such as Documents) and does not allow other apps or your PC to access its files. At least not by any official means. If you want to transfer your file to your PC, then what you can do is have your app e-mail it to yourself via our
native.showPopup("mail")
API as documented via the link below. This API allows you to attach this file.
http://docs.coronalabs.com/api/library/native/showPopup.html

Regarding the
display.catpure()
function, it was designed to save to the platform's Photo Gallery, making it publicly available. This would make your screenshot available to your PC via iTunes. However, iOS does not allow you to set the file name of the photo. That is an iOS limitation, which is why our API does not allow you to set it. If you absolutely must set the file name, then your only option is the
display.save()
function, but then it is saved to your app's sandbox. Each has their own limitations, but that's just the way Apple made it.

Anyways, I hope this helps.
uid: 32256 topic_id: 15661 reply_id: 136383


[TOPIC: post.html]
#47

ali.ayyaz

[GLOBAL: userInfoPane.html]
ali.ayyaz
  • Enthusiast

  • 60 posts
  • Corona SDK

Yes, that was well explained and helpful. Thanks Joshua.
uid: 175611 topic_id: 15661 reply_id: 136556



[topic_controls]
Page 2 of 2 1 2
 
[/topic_controls]