/// <summary>
        /// Called when activity started with StartActivityForResult() returns.
        /// </summary>
        /// <param name="requestCode">request code used in StartActivityForResult()</param>
        /// <param name="resultCode">result code</param>
        /// <param name="data">intent data from file picking</param>
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Canceled)
            {
                // Notify user file picking was cancelled.
                OnFilePickCancelled();
                this.Finish();
            }
            else
            {
                try
                {
                    if (data?.Data == null)
                    {
                        throw new Exception("File picking returned no valid data");
                    }

                    System.Diagnostics.Debug.Write(data.Data);

                    var uri = data.Data;

                    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
                    {
                        context.ContentResolver.TakePersistableUriPermission(
                            uri,
                            ActivityFlags.GrantReadUriPermission);
                    }

                    var filePath = IOUtil.GetPath(this.context, uri);

                    if (string.IsNullOrEmpty(filePath))
                    {
                        filePath = IOUtil.IsMediaStore(uri.Scheme) ? uri.ToString() : uri.Path;
                    }

                    var fileName = this.GetFileName(this.context, uri);

                    OnFilePicked(new FilePickerEventArgs(fileName, filePath));
                }
                catch (Exception readEx)
                {
                    System.Diagnostics.Debug.Write(readEx);

                    // Notify user file picking failed.
                    FilePickCancelled?.Invoke(
                        this,
                        new FilePickerCancelledEventArgs
                    {
                        Exception = readEx
                    });
                }
                finally
                {
                    this.Finish();
                }
            }
        }
 private static void OnFilePickCancelled()
 {
     try
     {
         FilePickCancelled?.Invoke(null, null);
     }
     catch (Exception)
     {
     }
 }
Esempio n. 3
0
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Canceled)
            {
                // Notify user file picking was cancelled.
                OnFilePickCancelled();
                Finish();
            }
            else
            {
                System.Diagnostics.Debug.Write(data.Data);
                try {
                    var _uri = data.Data;

                    var filePath = IOUtil.getPath(context, _uri);

                    if (string.IsNullOrEmpty(filePath))
                    {
                        filePath = IOUtil.isMediaStore(_uri.Scheme) ? _uri.ToString() : _uri.Path;
                    }
                    byte[] file;
                    if (IOUtil.isMediaStore(_uri.Scheme))
                    {
                        file = IOUtil.readFile(context, _uri);
                    }
                    else
                    {
                        file = IOUtil.readFile(filePath);
                    }

                    var fileName = GetFileName(context, _uri);

                    OnFilePicked(new FilePickerEventArgs(file, fileName, filePath));
                } catch (Exception readEx) {
                    System.Diagnostics.Debug.Write(readEx);
                    // Notify user file picking failed.
                    FilePickCancelled?.Invoke(
                        this,
                        new FilePickerCancelledEventArgs
                    {
                        Exception = readEx
                    });
                } finally {
                    Finish();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Called when activity started with StartActivityForResult() returns.
        /// </summary>
        /// <param name="requestCode">request code used in StartActivityForResult()</param>
        /// <param name="resultCode">result code</param>
        /// <param name="data">intent data from file picking</param>
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Canceled)
            {
                // Notify user file picking was cancelled.
                OnFilePickCancelled();
                this.Finish();
            }
            else
            {
                try
                {
                    if (data?.Data == null)
                    {
                        throw new Exception("File picking returned no valid data");
                    }

                    System.Diagnostics.Debug.Write(data.Data);

                    var uri = data.Data;

                    OnFilePicked(uri);
                }
                catch (Exception readEx)
                {
                    System.Diagnostics.Debug.Write(readEx);

                    // Notify user file picking failed.
                    FilePickCancelled?.Invoke(
                        this,
                        new FilePickerCancelledEventArgs
                    {
                        Exception = readEx
                    });
                }
                finally
                {
                    this.Finish();
                }
            }
        }
Esempio n. 5
0
 private static void OnFilePickCancelled()
 {
     FilePickCancelled?.Invoke(null, null);
 }
Esempio n. 6
0
 public override void OnBackPressed()
 {
     base.OnBackPressed();
     FilePickCancelled?.Invoke(null, null);
 }
        /// <summary>
        /// Called when activity started with StartActivityForResult() returns.
        /// </summary>
        /// <param name="requestCode">request code used in StartActivityForResult()</param>
        /// <param name="resultCode">result code</param>
        /// <param name="data">intent data from file picking</param>
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Canceled)
            {
                // Notify user file picking was cancelled.
                OnFilePickCancelled();
                this.Finish();
            }
            else
            {
                try
                {
                    if (data?.Data == null)
                    {
                        throw new Exception("File picking returned no valid data");
                    }

                    System.Diagnostics.Debug.Write(data.Data);

                    var uri = data.Data;

                    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
                    {
                        context.ContentResolver.TakePersistableUriPermission(
                            uri,
                            ActivityFlags.GrantReadUriPermission);
                    }

                    // The scoped storage feature was introduced on Android Q and it prevents direct file access on external storage.
                    // Thus, using the IOUtil.GetPath should be avoided because a System.UnauthorizedAccessException
                    // will be thrown when calling File.OpenRead() on the local path.
                    // For more information, see: https://developer.android.com/training/data-storage/#scoped-storage
                    var filePath = (int)Build.VERSION.SdkInt >= 29
                        ? uri.ToString()
                        : IOUtil.GetPath(this.context, uri);

                    if (string.IsNullOrEmpty(filePath))
                    {
                        filePath = IOUtil.IsMediaStore(uri.Scheme) ? uri.ToString() : uri.Path;
                    }

                    var fileName   = this.GetFileName(this.context, uri);
                    var folderPath = this.GetFolderPath(this.context, uri);

                    OnFilePicked(new FilePickerEventArgs(fileName, filePath));
                }
                catch (Exception readEx)
                {
                    System.Diagnostics.Debug.Write(readEx);

                    // Notify user file picking failed.
                    FilePickCancelled?.Invoke(
                        this,
                        new FilePickerCancelledEventArgs
                    {
                        Exception = readEx
                    });
                }
                finally
                {
                    this.Finish();
                }
            }
        }