Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
c0c1b87e
Commit
c0c1b87e
authored
Apr 09, 2018
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement more reliable way to get file size
parent
aa4e1bc8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
13 deletions
+16
-13
Uri.kt
app/src/main/java/chat/rocket/android/util/extensions/Uri.kt
+16
-13
No files found.
app/src/main/java/chat/rocket/android/util/extensions/Uri.kt
View file @
c0c1b87e
...
@@ -9,10 +9,7 @@ import android.provider.DocumentsContract
...
@@ -9,10 +9,7 @@ import android.provider.DocumentsContract
import
android.provider.MediaStore
import
android.provider.MediaStore
import
android.provider.OpenableColumns
import
android.provider.OpenableColumns
import
android.webkit.MimeTypeMap
import
android.webkit.MimeTypeMap
import
java.io.FileInputStream
import
java.io.*
import
java.io.FileNotFoundException
import
java.io.IOException
import
java.io.InputStream
fun
Uri
.
getFileName
(
context
:
Context
):
String
?
{
fun
Uri
.
getFileName
(
context
:
Context
):
String
?
{
val
cursor
=
context
.
contentResolver
.
query
(
this
,
null
,
null
,
null
,
null
,
null
)
val
cursor
=
context
.
contentResolver
.
query
(
this
,
null
,
null
,
null
,
null
,
null
)
...
@@ -27,16 +24,22 @@ fun Uri.getFileName(context: Context): String? {
...
@@ -27,16 +24,22 @@ fun Uri.getFileName(context: Context): String? {
}
}
fun
Uri
.
getFileSize
(
context
:
Context
):
Int
{
fun
Uri
.
getFileSize
(
context
:
Context
):
Int
{
val
cursor
=
context
.
contentResolver
.
query
(
this
,
null
,
null
,
null
,
null
,
null
)
var
fileSize
:
String
?
=
null
if
(
scheme
==
ContentResolver
.
SCHEME_CONTENT
)
{
val
fileSize
=
cursor
?.
use
{
try
{
val
sizeIndex
=
cursor
.
getColumnIndex
(
OpenableColumns
.
SIZE
)
val
fileInputStream
=
context
.
contentResolver
.
openInputStream
(
this
)
if
(
cursor
.
moveToFirst
())
{
fileSize
=
fileInputStream
.
available
().
toString
()
if
(!
cursor
.
isNull
(
sizeIndex
))
{
}
catch
(
e
:
Exception
)
{
return
@use
cursor
.
getString
(
sizeIndex
)
e
.
printStackTrace
()
}
}
}
else
if
(
scheme
==
ContentResolver
.
SCHEME_FILE
)
{
val
path
=
this
.
path
try
{
val
f
=
File
(
path
)
fileSize
=
f
.
length
().
toString
()
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
}
}
return
@use
null
}
}
return
fileSize
?.
toIntOrNull
()
?:
-
1
return
fileSize
?.
toIntOrNull
()
?:
-
1
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment