Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Save image to CameraRoll WP8

Discussion in 'Windows' started by trunkz, Oct 8, 2014.

  1. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    Hi, i got an problem when save my image after took a photo to Camera Roll ?
    So anyone can help me ?
     
  2. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    anyone ?
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    We could try to help if you said what the problem is? What are you trying to do and how?
     
  4. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    Im trying to use Device's camre to take a photo and save it, but after take a shot, it's cannot save. here the code:
    Code (CSharp):
    1. public string deviceName;
    2.     WebCamTexture wct;
    3.     public string path = "";
    4.     string fileName = "photo.PNG";
    5.     //UnitypluginForWindowsPhone.MainPage cm;
    6.     void Start ()
    7.     {
    8.         WebCamDevice[] devices = WebCamTexture.devices;
    9.         deviceName = devices [0].name;
    10.         wct = new WebCamTexture (deviceName, 1280, 720, 60);    
    11.         renderer.material.mainTexture = wct;
    12.         wct.Play ();
    13.         #if UNITY_ANDROID
    14.         path = Application.persistentDataPath;
    15.         #elif UNITY_WP8
    16.         path = "\\Windows Phone\\SD card\\";
    17.         #endif
    18.  
    19.     }
    20.  
    21.     void TakePhoto ()
    22.     {
    23.  
    24.         //cm.Button_Click ();
    25.         Texture2D photo = new Texture2D (wct.width, wct.height);
    26.         photo.SetPixels (wct.GetPixels ());
    27.         photo.Apply ();
    28.         byte[] bytes = photo.EncodeToPNG ();
    29.         fileName = System.DateTime.Now.ToString () + ".PNG";
    30.  
    31.         FileStream fs = new FileStream(path + fileName, FileMode.CreateNew);
    32.         BinaryWriter w = new BinaryWriter(fs);
    33.         w.Write(bytes);
    34.  
    35.         fs.Close ();
    36.  
    37.     }
    38.  
    39.     void OnGUI ()
    40.     {
    41.         if (GUI.Button (new Rect (5, 5, 120, 60), "Shot!")) {
    42.             TakePhoto ();
    43.         }
    44.         GUI.Label (new Rect (100, 5, 80, 100), path + fileName);
    45.     }
    46.  
     
    Last edited: Oct 8, 2014
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    I think you have multiple problems here.
    First, try to extract all code you use to save file to the simple Windows Phone C# application and check if it works there.
    Check you phone capabilities, I'm not sure you can save files to SD card so simply.
    Finally, do you get any errors when do this? It would be easier to detect errors, if you caught exceptions yourself and printed them out, as Visual Studio sometimes eats the useful information.
     
  6. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    i checked capabilities and edited "path" (it's not must be SD card), it's ok, i think the error gonna be the "path", may be it's wrong but i don't know what it have to be.
     
  7. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    And it work with Android !
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Hi,

    the problem is definitely with the path. I'll try looking for the path you need.
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    I think you have to use File Picker to save to SD card. Generaly you can save files to isolated storage, but that is specific for an app.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Indeed, it seems applications cannot write directly to the camera roll - probably for security reasons (so you couldn't overwrite user photos).

    The path in question is "C:\Data\Users\Public\Camera Roll", but you can only get read-only access rights to that folder. The solution would be to write a plugin which would do this through system specific APIs.

    P. S. You could save it to persistentDataPath, but user has no way to access that outside of your application.
     
  11. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    Thanks sir, you mean i cannot save image right to the camera roll, so i have to write a plugin !
     
    Last edited: Oct 9, 2014
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Windows Phone Applications are running in a sandbox and have limited privileges. They can save files to their own storage, but have limitations on athe rest.
    You can write a plugin that would use File Picker, so you'd get users permission to save somewhere.
     
  13. jijieciprian

    jijieciprian

    Joined:
    Dec 18, 2015
    Posts:
    7
    Does anyone have a plugin for windows phone to save images to pictures? Thx.