locked
Capture photo and save with specific name RRS feed

  • Question

  • Hi, I want to save the photo I captured with a specific name. Here are two ways I have tried:

    1  use the following code to save the file in a different location:

     StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
                        {
                            BitmapImage bitmapImage = new BitmapImage();
                            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
                            {
                                bitmapImage.SetSource(fileStream);
                            }
                            
                            StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                            var result = await file.CopyAsync(storageFolder, Filename);
    
                            // Store the file path in Application Data
                            appSettings[photoKey] = file.Path;
    
                            
                        }
                        

    Here I got error message:  access denied.

    2   save the picture in default path which is under appdata file and rename it, here is the code:

    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
                        StorageFile logFile = null;
                        try
                        {
                            StorageFolder currentFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                            logFile = await currentFolder.GetFileAsync(file.Name);
    
                            if (logFile != null)
                            {
                                await logFile.RenameAsync(Filename);
                            }
                        }
                        catch (Exception)
                        {
                        }

    this method doesn't work because when I set break point, I found that logFile can't get the file and always has the value "Null", 

    this is because “

     Windows.Storage.ApplicationData.Current.LocalFolder

    ” point to "Local state" folder but the picture actually saved in "tempstate" folder

    logFile = await currentFolder.GetFileAsync(file.Name);
    can anyone help me to figure it out? many thanks

    • Edited by Jenny730 Thursday, October 24, 2013 10:15 PM
    Thursday, October 24, 2013 9:44 PM

Answers

  • Hi,Jenny730

    If you already get the file, you can copy it to the folder that you want to have the picture. Try

    some codes below:

    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
    StorageFile fileCopy = await file.CopyAsync(KnownFolders.PicturesLibrary, "newfiename.png", NameCollisionOption.ReplaceExisting); 
    }

    In order to use this folder, you need to add the PictureLibrary capability to your manifest,

    otherwise you would get an access denied exception.

    Also, if you want to get file in temporary folder,you can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh700363.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Friday, October 25, 2013 7:04 AM from
    • Marked as answer by Anne Jing Thursday, October 31, 2013 7:07 AM
    Friday, October 25, 2013 7:04 AM

All replies

  • Hi,Jenny730

    If you already get the file, you can copy it to the folder that you want to have the picture. Try

    some codes below:

    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
    StorageFile fileCopy = await file.CopyAsync(KnownFolders.PicturesLibrary, "newfiename.png", NameCollisionOption.ReplaceExisting); 
    }

    In order to use this folder, you need to add the PictureLibrary capability to your manifest,

    otherwise you would get an access denied exception.

    Also, if you want to get file in temporary folder,you can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh700363.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Friday, October 25, 2013 7:04 AM from
    • Marked as answer by Anne Jing Thursday, October 31, 2013 7:07 AM
    Friday, October 25, 2013 7:04 AM
  • Hi ,

    In addition to all of the above you can always create your own folder and grant some privileges.

     private StorageFolder m_downloadfolder;

     public StorageFolder Download_Folder
            {
                get { return m_downloadfolder; }
                set { m_downloadfolder = value; }
            }

      if (StorageApplicationPermissions.FutureAccessList.Entries.Count() >= 1000)
                    {
                        StorageApplicationPermissions.FutureAccessList.Clear();
                    }

                    // Check if we create this folder before, if not create it
                    if (StorageApplicationPermissions.FutureAccessList.Entries.Any(item => item.Metadata == "myfolder"))
                    {
                        // if we have the folder in the FutureAccessList, just ui
                        var entry = StorageApplicationPermissions.FutureAccessList.Entries.FirstOrDefault(item => item.Metadata == "myfolder");
                        Download_Folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(entry.Token);
                    }
                    else
                    {
                        Download_Folder = await DownloadsFolder.CreateFolderAsync("myfolder");
                        StorageApplicationPermissions.FutureAccessList.Add(Download_Folder, "myfolder");
                    }

    thank you

    Friday, October 25, 2013 7:25 AM