예제 #1
0
            FileInfo SaveResult(Uri uri)
            {
                var result = IO.CreateTempDirectory(globalCache: false).GetFile($"File.{ShortGuid.NewGuid()}." + "mp4".OnlyWhen(IsVideo).Or("jpg"));

                var fileUri = uri ?? FileUrlPath;

                if (fileUri?.Scheme == "file")
                {
                    var file = new System.Uri(fileUri.ToString()).LocalPath;
                    if (file != result.FullName)
                    {
                        File.Copy(file, result.FullName);
                    }
                }
                else if (fileUri?.Scheme == "content")
                {
                    SaveContentToFile(fileUri, result);
                }

                try { if (PurgeCameraRoll)
                      {
                          ContentResolver.Delete(fileUri, null, null);
                      }
                }
                catch { }

                return(result);
            }
        public void ShareWebLink(System.Uri link, string packageName)
        {
            if (link != null)
            {
                Intent share = new Intent(Intent.ActionSend);
                share.SetType("text/*");
                share.PutExtra(Intent.ExtraText, $"{link.ToString()}");
                share.SetFlags(ActivityFlags.NewTask);

                try
                {
                    Android.App.Application.Context.StartActivity(share);
                }
                catch (Exception e)
                {
                    HandleError.Process("CommonShareService", "ShareWebLink", e, false);
                }
            }
        }
예제 #3
0
        internal static Task <Tuple <string, string, bool> > GetFileForUriAsync(Context context, Uri uri, bool isPhoto, bool saveToAlbum)
        {
            var tcs = new TaskCompletionSource <Tuple <string, string, bool> >();

            if (uri.Scheme == "file")
            {
                var path             = new System.Uri(uri.ToString()).LocalPath;
                var originalFilename = Path.GetFileName(path);
                tcs.SetResult(new Tuple <string, string, bool>(path, originalFilename, false));
            }
            else if (uri.Scheme == "content")
            {
                Task.Factory.StartNew(() =>
                {
                    ICursor cursor = null;
                    try
                    {
                        string[] proj = null;
                        if ((int)Build.VERSION.SdkInt >= 22)
                        {
                            proj = new[] { MediaStore.MediaColumns.Data }
                        }
                        ;

                        cursor = context.ContentResolver.Query(uri, proj, null, null, null);
                        if (cursor == null || !cursor.MoveToNext())
                        {
                            tcs.SetResult(new Tuple <string, string, bool>(null, null, false));
                        }
                        else
                        {
                            var column         = cursor.GetColumnIndex(MediaStore.MediaColumns.Data);
                            string contentPath = null;

                            if (column != -1)
                            {
                                contentPath = cursor.GetString(column);
                            }

                            string originalFilename = null;

                            // If they don't follow the "rules", try to copy the file locally
                            if (contentPath == null || !contentPath.StartsWith("file", StringComparison.InvariantCultureIgnoreCase))
                            {
                                string fileName = null;
                                try
                                {
                                    fileName         = Path.GetFileName(contentPath);
                                    originalFilename = fileName;
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine("Unable to get file path name, using new unique " + ex);
                                }


                                var outputPath = GetOutputMediaFile(context, "temp", fileName, isPhoto, false);

                                try
                                {
                                    using (var input = context.ContentResolver.OpenInputStream(uri))
                                        using (var output = File.Create(outputPath.Path))
                                            input.CopyTo(output);

                                    contentPath = outputPath.Path;
                                }
                                catch (Java.IO.FileNotFoundException fnfEx)
                                {
                                    // If there's no data associated with the uri, we don't know
                                    // how to open this. contentPath will be null which will trigger
                                    // MediaFileNotFoundException.
                                    System.Diagnostics.Debug.WriteLine("Unable to save picked file from disk " + fnfEx);
                                }
                            }
                            else
                            {
                                originalFilename = Path.GetFileName(contentPath);
                            }

                            tcs.SetResult(new Tuple <string, string, bool>(contentPath, originalFilename, false));
                        }
                    }
                    finally
                    {
                        if (cursor != null)
                        {
                            cursor.Close();
                            cursor.Dispose();
                        }
                    }
                }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
            }
            else
            {
                tcs.SetResult(new Tuple <string, string, bool>(null, null, false));
            }

            return(tcs.Task);
        }