2015-07-15 1 views
0

Je veux partager la capture d'écran de mon application. La capture d'écran est enregistrée dans la bibliothèque d'images du téléphone, puis elle est partagée en tant que StorageFilePartage de capture d'écran sur Windows Phone 8.1 WinRT ne joint pas d'image sur le partage d'applications

Le problème est que l'image n'est pas jointe aux applications de partage. J'ai vérifié que la capture d'écran a été enregistrée avec succès dans la bibliothèque d'images du téléphone.

Voici mon code. Qu'est-ce que je rate?

private async void askFacebook() 
    { 
     // Render some UI to a RenderTargetBitmap 
     var renderTargetBitmap = new RenderTargetBitmap(); 
     await renderTargetBitmap.RenderAsync(this.gridRoot, (int)this.gridRoot.ActualWidth, (int)this.gridRoot.ActualHeight); 

     // Get the pixel buffer and copy it into a WriteableBitmap 
     var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); 
     var width = renderTargetBitmap.PixelWidth; 
     var height = renderTargetBitmap.PixelHeight; 
     var wbmp = await new WriteableBitmap(1, 1).FromPixelBuffer(pixelBuffer, width, height); 

     imageToShare = await saveWriteableBitmapAsJpeg(wbmp, string.Format("{0}.jpg", getAppTitle())); 
     DataTransferManager.ShowShareUI(); 
    } 

    private string getAppTitle() 
    { 
     // Get the assembly with Reflection: 
     Assembly assembly = typeof(App).GetTypeInfo().Assembly; 

     // Get the custom attribute informations: 
     var titleAttribute = assembly.CustomAttributes.Where(ca => ca.AttributeType == typeof(AssemblyTitleAttribute)).FirstOrDefault(); 

     // Now get the string value contained in the constructor: 
     return titleAttribute.ConstructorArguments[0].Value.ToString(); 
    } 

    private async Task<StorageFile> saveWriteableBitmapAsJpeg(WriteableBitmap bmp, string fileName) 
    { 
     // Create file in Pictures library and write jpeg to it 
     var outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); 
     using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) 
     { 
      await encodeWriteableBitmap(bmp, writeStream, BitmapEncoder.JpegEncoderId); 
     } 
     return outputFile; 
    } 

    private async Task encodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId) 
    { 
     // Copy buffer to pixels 
     byte[] pixels; 
     using (var stream = bmp.PixelBuffer.AsStream()) 
     { 
      pixels = new byte[(uint)stream.Length]; 
      await stream.ReadAsync(pixels, 0, pixels.Length); 
     } 

     // Encode pixels into stream 
     var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream); 
     var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi; 

     encoder.SetPixelData(BitmapPixelFormat.Bgra8, 
      BitmapAlphaMode.Premultiplied, 
      (uint)bmp.PixelWidth, 
      (uint)bmp.PixelHeight, 
      logicalDpi, 
      logicalDpi, 
      pixels); 

     await encoder.FlushAsync(); 
    } 


    private void ShareImageHandler(DataTransferManager sender, DataRequestedEventArgs e) 
    { 
     DataRequest request = e.Request; 
     request.Data.Properties.Title = "Ask Social Media"; 
     request.Data.Properties.Description = "Do you know the answer to this question?"; 

     // Because we are making async calls in the DataRequested event handler, 
     // we need to get the deferral first. 
     DataRequestDeferral deferral = request.GetDeferral(); 

     // Make sure we always call Complete on the deferral. 
     try 
     { 
      request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageToShare)); 
     } 
     finally 
     { 
      deferral.Complete(); 
     } 
    } 

Répondre

0

Apparemment pour Windows Phone, l'image doit être un StorageItem que les méthodes ne fonctionne que SetBitmap avec Windows 8.x

Donc, pour Windows Phone, au lieu de

request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageToShare)); 

J'ai créé un élément de stockage et utilisez SetStorageItems pour le partager. Il fonctionne avec des applications Windows Phone natives telles que les e-mails, OneNote mais je ne l'ai pas testé pour le partager sur Facebook, Twitter, etc.

var imageItems = new List<IStorageItem>(); 
imageItems.Add(imageToShare); 
request.Data.SetStorageItems(imageItems); 
+0

est possible de convertir le flux en IStorageItem? voir mon code actuel s'il vous plaît https://gist.github.com/jdnichollsc/619f9db2c3e61667ea35 – jdnichollsc