Пример #1
0
        public async Task <IFileStats> GetFileStats(CancellationToken cancellationToken = default(CancellationToken))
        {
            var name   = Name;
            var length = file.Length();

            return(new AndroidFileStats
            {
                Name = System.IO.Path.GetFileNameWithoutExtension(name),
                Extension = System.IO.Path.GetExtension(name),
                Length = length,
            });
        }
Пример #2
0
        /// <summary>
        /// Retrieves basic properties for a given SAF Uri.
        /// </summary>
        /// <param name="safUri">SAF Uri.</param>
        /// <param name="includeSize">A value indicating whether the size should be included (not useful for folders).</param>
        /// <param name="token">Cancellation token.</param>
        /// <returns>Basic properties.</returns>
        public static async Task <BasicProperties> GetBasicPropertiesAsync(Android.Net.Uri safUri, DocumentFile documentFile, bool includeSize, CancellationToken token)
        {
            if (Application.Context.ContentResolver == null)
            {
                throw new InvalidOperationException("Content resolver for app is not available.");
            }

            return(await Task.Run(() =>
            {
                var cursor = Application.Context.ContentResolver.Query(safUri, null, null, null, null, null);
                if (cursor == null)
                {
                    // Try to retrieve the info directly from DocumentFile.
                    var size = includeSize ? documentFile.Length() : 0L;
                    var lastModifiedTimestamp = documentFile.LastModified();
                    var lastModified = DateTimeOffset.FromUnixTimeMilliseconds(lastModifiedTimestamp);
                    return new BasicProperties((ulong)size, lastModified);
                }

                using (cursor)
                {
                    if (cursor.MoveToFirst())
                    {
                        int size = 0;
                        var sizeColumnIndex = cursor.GetColumnIndex(DocumentsContract.Document.ColumnSize);
                        if (includeSize && sizeColumnIndex >= 0)
                        {
                            size = cursor.GetInt(sizeColumnIndex);
                        }
                        var lastModified = DateTimeOffset.MinValue;
                        var lastModifiedIndex = cursor.GetColumnIndex(DocumentsContract.Document.ColumnLastModified);
                        if (lastModifiedIndex >= 0)
                        {
                            var lastModifiedTimestamp = cursor.GetLong(lastModifiedIndex);
                            lastModified = DateTimeOffset.FromUnixTimeMilliseconds(lastModifiedTimestamp);
                        }
                        return new BasicProperties((ulong)size, lastModified);
                    }
                }
                return new BasicProperties(0, DateTimeOffset.MinValue);
            }, token));
        }