2017-02-09 77 views
1

我试图创建一个功能,让我分享能够通过androids共享对话,共享图像到任何支持申请例如。松弛,Outlook等Xamarin Android - 共享图像从应用程序结果没有图像显示,并在Android的Outlook IO错误

但是,当我运行下面的代码完成没有错误的图像不会出现在我分享给任何应用程序和Outlook来回android给出一个通用的IO错误。

async void shareMetricFromURL(){ 
       WebClient webClient = new WebClient(); 
       var url = new Uri ("https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/500px-Stack_Overflow_logo.svg.png"); 
       byte[] bytes = null; 


       try{ 
        bytes = await webClient.DownloadDataTaskAsync(url); 
       } 
       catch(TaskCanceledException){ 
        return; 
       } 
       catch(Exception e){ 
        return; 
       } 

       string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString(); 
       string localFilename = "downloaded.png"; 
       string localPath = System.IO.Path.Combine (documentsPath, localFilename); 

       //Sive the Image using writeAsync 
       FileStream fs = new FileStream (localPath, FileMode.OpenOrCreate); 
       await fs.WriteAsync (bytes, 0, bytes.Length); 

       fs.Close(); 

       var sendIntent = new Intent(); 
       sendIntent.SetAction(Intent.ActionSend); 

       var uri = Android.Net.Uri.FromFile(new Java.IO.File(localPath)); 
       sendIntent.PutExtra(Intent.ExtraStream, uri); 

       sendIntent.SetType("image/*"); 
       StartActivityForResult(Intent.CreateChooser(sendIntent, "Share With..."), 0); 
    } 

有什么想法?

回答

0

尝试添加

sendIntent.AddFlags (ActivityFlags.GrantReadUriPermission); 

所以文件可以读取

见这个例子

public void Send() 
{ 


    Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.icon_120); 

    var tempFilename = "test.png"; 
    var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; 
    var filePath = System.IO.Path.Combine(sdCardPath, tempFilename); 
    using (var os = new FileStream(filePath, FileMode.Create)) 
    { 
     b.Compress(Bitmap.CompressFormat.Png, 100, os); 
    } 
    b.Dispose(); 

    var imageUri = Android.Net.Uri.Parse ($"file://{sdCardPath}/{tempFilename}"); 
    var sharingIntent = new Intent(); 
    sharingIntent.SetAction (Intent.ActionSend); 
    sharingIntent.SetType ("image/*"); 

    sharingIntent.PutExtra (Intent.ExtraStream, imageUri); 
    sharingIntent.AddFlags (ActivityFlags.GrantReadUriPermission); 
    StartActivity (Intent.CreateChooser (sharingIntent, "choose"));} 
相关问题