Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip files that cannot be read #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import androidx.lifecycle.*
import com.slavabarkov.tidy.R
import com.slavabarkov.tidy.centerCrop
Expand All @@ -23,6 +24,7 @@ import com.slavabarkov.tidy.normalizeL2
import com.slavabarkov.tidy.preProcess
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.IOException
import java.util.*

class ORTImageViewModel(application: Application) : AndroidViewModel(application) {
Expand All @@ -32,6 +34,8 @@ class ORTImageViewModel(application: Application) : AndroidViewModel(application
var embeddingsList: ArrayList<FloatArray> = arrayListOf()
var progress: MutableLiveData<Double> = MutableLiveData(0.0)

private val TAG: String = ORTImageViewModel::class.java.simpleName

init {
val imageEmbeddingDao = ImageEmbeddingDatabase.getDatabase(application).imageEmbeddingDao()
repository = ImageEmbeddingRepository(imageEmbeddingDao)
Expand Down Expand Up @@ -74,8 +78,16 @@ class ORTImageViewModel(application: Application) : AndroidViewModel(application
} else {
val imageUri: Uri = Uri.withAppendedPath(uri, id.toString())
val inputStream = contentResolver.openInputStream(imageUri)
val bytes = inputStream?.readBytes()
inputStream?.close()
val bytes: ByteArray?

try {
bytes = inputStream?.readBytes()
} catch (ex: IOException) {
Log.w(TAG, "IOException occurred while reading file (ID: $id, date: $date, bucket: \"$bucket\")")
continue
} finally {
inputStream?.close()
}

// Can fail to create the image decoder if its not implemented for the image type
val bitmap: Bitmap? =
Expand Down