2
votes

I am writing an Android app in Unity C# which implements an sqlite database. The program tries to store pictures in the database with relevant info.

I am trying to solve the following steps to solve a piece of this:

  1. Display a png as 'texture2d' located in the Resources folder - achieved
  2. Convert the png 'texture2d' to a byte array (to save to a db) -???
  3. Convert from byte array back into a 'Texture2D' and display it (PROBLEMS HERE).

At step 3, when trying to convert the byte array back into a 'texture2d', something goes wrong. A red question mark is displayed instead of the png.

The variable 'img' is supposed to hold the texture converted back from the byte array, but trying to display 'img' just shows a big red question mark. Any help would be greatly appreciated.

public byte[] imageData;    //image byte array that wil hold texture-converted-to-png
/******************/

// Use this for initialization
void Start () {

    /************************/

    // load texture from resource folder
    Texture2D photo = new Texture2D(355, 355);  //declare texture2d to hold pic from resources folder
    Texture2D img = new Texture2D(355, 355);    //holds texture converted back from byte array 

    photo = Resources.Load("eeyore") as Texture2D; //load pic from resources folder into texture2d

    imageData = photo.EncodeToPNG();    //Encode texture to PNG to save as Byte Array in database
    img.LoadImage(imageData);           //try to change byte array back into a texture2d to display
    img.Apply();                        //same thing happens if this is here or not

    GameObject rawImage = GameObject.Find("RawImage"); //Find the 'RawImage' 
    rawImage.GetComponent<RawImage>().texture = img;   //BIG RED ? HERE ...set a texture to the raw image ('photo' works,'img' doesnt)

    /*************************/
    StartSync();
}

this is when i display 'photo': enter image description here

this is when i display 'img': enter image description here

3
its.png. But an extension is not needed. When i bring 'eeyore' in its saved in 'photo'. In the next to last line, if i use 'photo' instead of 'img', the image will display. - MattBorg
i try to change 'photo' to an array of bytes[], then back to a texture2d called 'img'. 'img' will not display, but its 'parent photo' will. - MattBorg
and my pics are in the 'Resources' folder - MattBorg
photo isn't null. If i try to display 'photo' it shows up just fine. After trying to convert 'photo' to a byte[] array, then back it won't show. - MattBorg
yes correct. that line works. - MattBorg

3 Answers

-1
votes
    byte[] vs = new byte[] { };

    string[] newbytesString = downloadId.Split(new string[] { "/" }, System.StringSplitOptions.RemoveEmptyEntries);
    for(int kl=0;kl<newbytesString.Length;kl++)
    {
        vs[kl] = (byte)int.Parse(newbytesString[kl]);
    }
    icon.sprite = downloadedImage;
    downloadedImage.texture.LoadImage(vs);
    icon.sprite.texture.LoadImage(vs);
    icon.sprite.texture.Apply();
-2
votes
public string downloadId;
public Sprite downloadedImage;
public Image icon;

public void DownloadAFile()
{
     Debug.LogError("DownloadAFile()");
     // Get the url associated with the uploadId
     /*new GetUploadedRequest().SetUploadId(downloadId).Send((response) =>
     {
         //pass the url to our coroutine that will accept the data
         StartCoroutine(DownloadImage(response.Url));
     });*/

    byte[] vs = new byte[] { };

    string[] newbytesString = downloadId.Split(new string[] { "/" }, System.StringSplitOptions.RemoveEmptyEntries);
    for(int kl=0;kl<newbytesString.Length;kl++)
    {
        vs[kl] = (byte)int.Parse(newbytesString[kl]);
    }
    icon.sprite = downloadedImage;
    downloadedImage.texture.LoadImage(vs);
    icon.sprite.texture.LoadImage(vs);
    icon.sprite.texture.Apply();
}
-7
votes

Try this:

string mybytes = "25521625522401674707370011001010025521906708667658777998101220131211111225181915202926313029262828323646393234443528284055414448495252523139576156506046515250255219067199912111224131324503328335050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050255192017801300130313402171317125519603100151111110000000012345678910112551960181160213324355440011251230417518334965619819773411320501291451618356617719321822092403651981141309102223242526373839404142525354555657586768697071727374838485868788899099100101102103104105106115116117118119120121122131132133134135136137138146147148149150151152153154162163164165166167168169170178179180181182183184185186194195196197198199200201202210211212213214215216217218225226227228229230231232233234241242243244245246247248249250255196031103111111111000000123456789101125519601811702124434754401211901231745334961865817971131934501298206614516117719393551822402198114209102236522253724123242526383940414253545556575867686970717273748384858687888990991001011021031041051061151161171181191201211221301311321331341351361371381461471481491501511521531541621631641651661671681691701781791801811821831841851861941951961971981992002012022102112122132142152162172182262272282292302312322332342422432442452462472482492502552180123102173170630240106401621531631952184321152221285548942491691740868431194188211211971807950238362922087851488680206201471291842451741117542183202237209651112101802258718621222412112169142185236421083810817013184199215816872167109289612621503920372129168131281171671332519325310684791981649206104176171326024042182168159241411852425501501021808514572521061901681522101101431114117017316011756831242272357297972192116718916114249614291697715311211318015974207152214230331362101081731311662062171946255012910175531482321211292550520016615292206217245162172109111672498178236158995520811541381671622514971169169239192911660965952298520918218615967861171254149184302584254148116332385978662105461481071631383215124612824421621617212550622141581402252419148936961237184131502188730134133184722116924525310610131832021371845725084137233193521373769317414447601172396631421232121685620042240106190174184209174200237111274219013924215417117117525273175492550608818435146176136139881352513417519812855215541239864594031165916820774203169125513849156852241528070115237801961902231499382101002341131834019110615513124721924825323216823815550158571152522321679818057234411238228101621722023336105491592252420218403177208333121749228224177233239891221961501822479117216519620212696164224319923586382121341531631931101362221161451005510120511568146114126180911695452617716848101101561421562261647794242181632071241312121562141246449193865723287181911437319018563185182146762478523576441072191201342412315322827942195165116186941791071681831501411781125020918395195214184731801712519524521421017521377878715021411714575713415720212217751138824520019342127741731711752527311024125506020017062272141341731068524018317253225140110247211651711401041831241272035176150115177392024859116171721912531061423324214025502501702024612321470132177129914320217357219191713113353935697781861458625141152421541001561532536243205201011271881041645916315016214021019902245242012147521692041392542401606925386225238462010943281041750491561410618118859216192115338162239372091023614012419021353160198208715215528861861577812910311133218998635158691188610433251170209928102291245615955394254181216108216188117355218153178479119411012216217624725172111202487429741972225621459164271451281981251711237212519630132288615723413513515822141165165200105941991312481142269113109355793205229186145941331721401041831272452042152912310924695251791683625316029170611071812152326452232531155393123718520768112163175227866519257352121541721528525237854711168171888116172250150911841911422201141952431724571944719164715134505986612132361471834411823116582987953737414715963252245127251232209801265283178301302651692091378264187166792471705815421610222511521915458121811221281961727322275269111871007317923382921741092012443221320551761072385599111111901508485205296147180229192345255712691182223207168168401321861426417496232113217194210121146171122222002509921018612713512319022158144821811366527420017561197232188104109239254206217198101305108219381753019511312414716712649154204241471312522538123119752672219151107991111681741317320945226181711402002061711342202291352358517419653173206105244226222585536124158641511599414912793312413618861252179862297924815717999149128169252901711071635821239253115538121619412717203222491255623392246175413617411323791140223413921018518977195752341152051001834619897341522122271974917020966100122254148816948221472356920152131215316725022412556783218103206621521612363565134232153123145214180116591311220314282242141061241951427788183221202041895913814616223620615988188681820188913524173223134236115113385061141232151360222107100156224961031811171581718218913373911477156141921682424122320452051421758121219523417310932192108718453163104222691711211092421762288630171166188162431614114622632317724446214910239971072013535165903477881711410018418518563198219872326325024917021823061311251584911261902042132301020621521647135245141912342724985165161200229119115131151841831728121157631841071571851567333971581251051982492077105242441976692633611917223610815325110122210281191672811722613055912139208415230148831148822112102212282281247169164162222194109452002348824431810512120108282301571281289611522215943234795598220871016739381721552161571633331353113310015039173624637301482491084323410854230577031150107163208238101183194253178228103144856223324653205198761209651143210186205151961621817035149318419769181541402092105319720215217469208158505418617810999239199252524117923420866202178181702202383138124792531681932132656115146204491872338812634100911691748225269192236122215703015415617220676931038943967212041191119055872912683154200241275910424712830601342051152475124955542352714914818459148224227189651701062474977451712052316542214071356011717415415211929153201751920511010116919896113732231542321106049401828996112881749038601431612550261941542510922310017718010320814010218542821483016823716794157692381871519617989244249371124332447917617011141201741436613673165952511362519174231176796110197681501971972211805920324652841587525120974197115332461861241514873504818242381062371646651912411821862550116246531237818310712022741271722011082550421451796154114131229145115143661553232211195217381211814913201165242511631784512342502532132058515420515096128103118641741613313117359612420914026134918124617111472714212742210881162091561131097422271501821421955657218797974341281902361178115687664519417816995188178962146882173190162174126224962812311917239696929202351852221981421571104661947256174183702101627052135510313086178159777539911711193083124192123261873913617323696391164199106110714281179581634317314154247811317218114205190102249981431875524814245197106195116918621011491342272347323015512211221179106551747517619413120923219210179205207153118101613583392448125312726244112244253156110247103155941101817545151230321512062157514866272258021925501651071552195841506365852373197185144245812751691481908487501037102208126180115931759114321721759118177168250148175126194351503244126128154214759110713983121681781981520623922024721925502141749979442115114292123812579218640791478138891116188170158149211732432212002281967121721915175162331221272162118913311952662132185461215211951332241254321110839138251146315455510013217727208142707174224117107104237537513616171881311472618380167165112982402381515310812224287118018612315324924752832412376911321624478186223152236024181201255031205255092205208718619020124250248153161312222550128472421661832502166322495202138437117713024813610325509111125502254852047925503118125104162185431581341999170181102623113544792032359222013215515878121312061384320715119811971224103711702411652011426285254117138126237231213104162189261891559132401242022081271991892072081275896255014389632229040174941999022115415448251106313612790158209137149178792225281931161827121109217173111254183240172175214224911215612250227222138437679240153134152532252818120819425244103255217";

byte[] bytes =mybytes;