fix file size parsing issue causing crash

Opening a PDF using Storage Access Framework (SAF) causes the app to
crash in some cases, when the file is not present on the device's
internal storage (for example, on Proton Drive).
This commit is contained in:
octocorvus 2023-06-28 03:07:48 +00:00 committed by Daniel Micay
parent 145fd9896c
commit bfb5e4a538
2 changed files with 6 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import android.text.Spanned
import android.text.format.Formatter
import android.text.style.StyleSpan
import android.util.Log
import androidx.core.database.getLongOrNull
import app.grapheneos.pdfviewer.R
import org.json.JSONException
@ -102,8 +103,9 @@ class DocumentPropertiesLoader(
val indexSize: Int = cursor.getColumnIndex(OpenableColumns.SIZE)
if (indexSize >= 0) {
val fileSize: Long = cursor.getString(indexSize).toLong()
collections[DocumentProperty.FileSize] = Formatter.formatShortFileSize(context, fileSize)
val fileSize = cursor.getLongOrNull(indexSize)
collections[DocumentProperty.FileSize] =
fileSize?.let { Formatter.formatFileSize(context, it) } ?: context.getString(R.string.unknown_file_size)
}
}
return collections

View File

@ -44,4 +44,6 @@
<string name="creator">Creator</string>
<string name="pdf_version">PDF version</string>
<string name="pages">Pages</string>
<string name="unknown_file_size">Unknown</string>
</resources>