locked
How to open document,video,music files in Windows store app? RRS feed

  • Question

  • I'm creating a metro app that connects to cloud providers ,i am able to download files into my app i'm just having a problem viewing them ,any help will be appreciated or reference via links.

    PLEASE........!

    19karabo91

    Thursday, September 19, 2013 1:49 PM

Answers

All replies

  • Thursday, September 19, 2013 2:32 PM
    Moderator
  • Hello 19karabo91,

    try the Launcher to open your files with default viewer. Like open pdf file using Reader, open video file using Default player. show this link for more briefly detail :

    Launcher

    Friday, September 20, 2013 4:33 AM
  • Hi ,

    Did you try this ?

     var success = await Windows.System.Launcher.LaunchFileAsync(MyFile);

                if (success)
                {
                    // File launched
                }
                else
                {
                   this.DisplayToast("Open FIle", "Cannot open file the File. ", 0);
                }

    the MyFile is StorageFile MyFile

    Here 's how I'm using it in my app.

    public async Task WriteOpenAnyFile(Byte[] mybytes, string myfilename)
            {
                try
                {
                    StorageFile sampleFile = null;

                    sampleFile = await this.Download_Folder.CreateFileAsync(myfilename, CreationCollisionOption.ReplaceExisting);

                    await WriteAndOpenFile(sampleFile, mybytes);
                    this.LaunchFile(sampleFile);

                }
                catch (Exception ex)
                {

                    throw ex;
                }

            }

      public async Task WriteAndOpenFile(StorageFile sampleFile, Byte[] mybytes)
            {
                await FileIO.WriteBytesAsync(sampleFile, mybytes);
               
            }

      public async void LaunchFile(StorageFile MyFile)
            {
                var success = await Windows.System.Launcher.LaunchFileAsync(MyFile);

                if (success)
                {
                    // File launched
                }
                else
                {
                   this.DisplayToast("Open FIle", "Cannot open file the File. ", 0);
                }
            
            }

    I'm calling first the WriteOpenAnyFile passing the bytes and the filename I want . When you download it you already have the bytes.

    Then  in my Download_folder which is a folder that I have create and give access permissions I'm doing the rest of the code.

    Hope this helped you

    • Proposed as answer by zakkar Friday, September 20, 2013 4:56 AM
    Friday, September 20, 2013 4:56 AM
  • Thanks this will really help me a lot ,can i see how you download your files from dropbox?

    19karabo91

    Friday, September 20, 2013 6:27 AM
  • Hi ,

    I'm not using dropbox , I have a WCF service and I;m using the background transfer

    Here 's an example

    http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61

    I found it only for the 8.1 version. Somewhere there will be the 8.0 version.

    But if you go to the samples page you will see other samples too.

    http://code.msdn.microsoft.com/windowsapps/site/search?query=download&f%5B0%5D.Value=download&f%5B0%5D.Type=SearchText&ac=4

    • Marked as answer by 19karabo91 Friday, September 20, 2013 11:06 AM
    Friday, September 20, 2013 8:19 AM
  • Thank you again at least i am getting somewhere , so how can i change this method to be used in winRT?

    public class FileSystemInfo
    {
        //...
    
        public byte[] Data { get; internal set; }
    
        public void Save(string path)
        {
            using (var fileStream = new FileStream(
                path, FileMode.Create, FileAccess.ReadWrite))
            {
                fileStream.Write(Data, 0, Data.Length);
            }            
        }
    }


    19karabo91

    Friday, September 20, 2013 9:18 AM
  •  

    Let's say that you already have the storagefile Myfile

    var stream = await MyFile.OpenReadAsync();

                    using (var dataReader = new DataReader(stream))
                    {
                        var bytes = new byte[stream.Size];
                        await dataReader.LoadAsync((uint)stream.Size);
                        dataReader.ReadBytes(bytes);
                      

                    }

    Now you have the bytes.

    Also take a look at here

    http://code.msdn.microsoft.com/windowsapps/site/search?query=file%20access&f%5B0%5D.Value=file%20access&f%5B0%5D.Type=SearchText&ac=4

    The file access sample will help you.

    Don't forget to mark as answered my posts.

    thank you

    • Marked as answer by 19karabo91 Friday, September 20, 2013 11:06 AM
    Friday, September 20, 2013 11:03 AM