Determine the mime type of Image file Browsed using media.selectPhoto()

I want to upload the image to server & for that i am using media.selectPhoto() , to browse the image file from mobile gallery.

Now  after successfully browse & image select , the Browsed image is stored in system.DocumnetDirectiory

Now the PROBLEM is how to know the MIME-Type or extension (.jpg ,.png , etc) of that File.

PLEASE HELP :

The following is the CODE i am using :

media.selectPhoto(

    {

        

        mediaSource = media.SavedPhotosAlbum,

        destination = { baseDir=system.DocumentsDirectory, filename=“DemoImage”, type=“image” } ,

        listener = imageSelectedListener,

    })

I did some tests and the image type is determined by the filename extension (e.g. .jpg, .jpeg, .png).

Even if the type specifies something different (e.g. filename=“image.jpg” but type=“image/png”), the file type being saved is still determined by the filename extension, i.e. it’s a JPEG in this example and not a PNG.

When I used filename=“DemoImage” as in your example, I got “file not found” error when reading it using file:read().

Here’s how I determine the image type:

[lua]local fname = “tmpImg.jpg”
local dir = system.TemporaryDirectory

local function readFile()
    local path = system.pathForFile( fname, dir )
    local file, errorString = io.open( path, “rb” )

    if not file then
        print( "File error: " … errorString )
    else
        local firstByte = string.byte(file:read(1))

        if firstByte==255 then
            print(“image/jpeg”)
        elseif firstByte==137 then
            print(“image/png”)
        elseif firstByte==71 then
            print(“image/gif”)
        elseif firstByte==73 or firstByte==77 then
            print(“image/tiff”)
        end

        io.close( file )
    end

    file = nil
end

local function imageSelectedListener( event )
    if event.completed then
        readFile()
    end
end

media.selectPhoto(
        {
            mediaSource = media.PhotoLibrary,
            listener = imageSelectedListener,
            destination = { baseDir=dir, filename=fname, type=“image” }
        })
[/lua]

Although the code considers gif and tiff, I couldn’t save those types.

Dave

Hi qwidave ,

Already i have done that by using hard Code extension like  “DemoImage.jpg” …

BUT WHAT IF WE DID NOT SPECIFY THE EXTENSION…

UNABLE TO PRETEND THE EXTENSION OF THAT IMAGE …

CORONA NEEDS TO SOLVE THIS !!!

I did some tests and the image type is determined by the filename extension (e.g. .jpg, .jpeg, .png).

Even if the type specifies something different (e.g. filename=“image.jpg” but type=“image/png”), the file type being saved is still determined by the filename extension, i.e. it’s a JPEG in this example and not a PNG.

When I used filename=“DemoImage” as in your example, I got “file not found” error when reading it using file:read().

Here’s how I determine the image type:

[lua]local fname = “tmpImg.jpg”
local dir = system.TemporaryDirectory

local function readFile()
    local path = system.pathForFile( fname, dir )
    local file, errorString = io.open( path, “rb” )

    if not file then
        print( "File error: " … errorString )
    else
        local firstByte = string.byte(file:read(1))

        if firstByte==255 then
            print(“image/jpeg”)
        elseif firstByte==137 then
            print(“image/png”)
        elseif firstByte==71 then
            print(“image/gif”)
        elseif firstByte==73 or firstByte==77 then
            print(“image/tiff”)
        end

        io.close( file )
    end

    file = nil
end

local function imageSelectedListener( event )
    if event.completed then
        readFile()
    end
end

media.selectPhoto(
        {
            mediaSource = media.PhotoLibrary,
            listener = imageSelectedListener,
            destination = { baseDir=dir, filename=fname, type=“image” }
        })
[/lua]

Although the code considers gif and tiff, I couldn’t save those types.

Dave

Hi qwidave ,

Already i have done that by using hard Code extension like  “DemoImage.jpg” …

BUT WHAT IF WE DID NOT SPECIFY THE EXTENSION…

UNABLE TO PRETEND THE EXTENSION OF THAT IMAGE …

CORONA NEEDS TO SOLVE THIS !!!