write raw png data to file?

139 views
Skip to first unread message

Mike Zero

unread,
Jan 2, 2021, 3:37:10 AM1/2/21
to DroidScript
Hi,

I get the raw png data from a http request.
How can I use these data as an Image?
I tryed:  app.WriteFile("test.png",data,"ASCII");
and CreateFile()... but the file is created and
has the exact size, but I cannot open the Image
or use it as an background image etc. ???

Thanks!

Steve Garman

unread,
Jan 2, 2021, 4:15:24 AM1/2/21
to DroidScript
The encoding you want certainly won't be ASCII. Try changing it to base64 and see if that helps

Mike Zero

unread,
Jan 2, 2021, 11:06:15 AM1/2/21
to DroidScript
hmm, nothing works? I tryed everything...

Steve Garman

unread,
Jan 2, 2021, 11:42:06 AM1/2/21
to DroidScript
Can you post an example of an http request that produces the type of rawbdata you are dealing with please?

Mike Zero

unread,
Jan 3, 2021, 4:53:23 AM1/3/21
to DroidScript
The link to get the PNG image is:  https://rki.marlon-lueckert.de/api/states-map 
and I get it with app.HttpRequest();
I receive the Image, but I cannot write it to a file??? 

Cemal

unread,
Jan 3, 2021, 5:53:06 AM1/3/21
to DroidScript
Maybe it's help you:

function OnStart() {
    const lay = app.CreateLayout("Linear", "VCenter,FillXY");
    
    image = app.AddImage( lay, null, 1, (DW()/DH())*1 )
    
    app.AddLayout(lay);
    
    .then(res=>res.blob())
    .then(res=>OnImage(res));
}

/* @data: blob type
function OnImage(data)
{
reader = new FileReader()
reader.onload = function() {
                // reader.result - base64 image
image.SetPixelData( reader.result );
                // Save sdcard as image name full quality jpg format.
image.Save( "/sdcard/image.jpg", 100 );
};
reader.readAsDataURL(data)
}
3 Ocak 2021 Pazar tarihinde saat 12:53:23 UTC+3 itibarıyla mike...@gmail.com şunları yazdı:

Mike Zero

unread,
Jan 3, 2021, 11:58:18 AM1/3/21
to DroidScript
failed to fetch... sorry?

Steve Garman

unread,
Jan 3, 2021, 12:29:26 PM1/3/21
to DroidScript
@Mike

It looks like Cemal opened a comment blick and didn't close it
I'm pretty sure this is what he meant

function OnStart()
{
   const lay = app.CreateLayout("Linear", "VCenter,FillXY")
   image = app.AddImage(lay, null, 1, (DW() / DH()) * 1)
   app.AddLayout(lay)
      .then(res => res.blob())
      .then(res => OnImage(res));
}

function OnImage(data)
{
   reader = new FileReader()
   reader.onload = function()
   {
      // reader.result - base64 image
      image.SetPixelData(reader.result)
      // Save sdcard as image name full quality jpg format.
      image.Save("/sdcard/image.jpg", 100)
   }
   reader.readAsDataURL(data)
}

Mike Zero

unread,
Jan 3, 2021, 1:13:13 PM1/3/21
to DroidScript
Now it works.  Thank you so much !!!
Reply all
Reply to author
Forward
0 new messages