android/src/main/java/chat/flyer/rnandroiduripath/RNAndroidURIPathModule.kt
package chat.flyer.rnandroiduripath import android.net.Uriimport android.provider.OpenableColumnsimport com.facebook.react.bridge.ReactApplicationContextimport com.facebook.react.bridge.ReactContextBaseJavaModuleimport com.facebook.react.bridge.ReactMethodimport okio.*import java.io.Fileimport java.io.FileNotFoundExceptionimport java.io.IOException class RNAndroidURIPathModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { override fun getName() = "RNAndroidURIPathModule" Method `getPath` has 28 lines of code (exceeds 25 allowed). Consider refactoring. @ReactMethod(isBlockingSynchronousMethod = true) fun getPath(uriString: String): String { val uri = Uri.parse(uriString) val cursor = reactApplicationContext.contentResolver.query(uri, null, null, null, null) cursor ?: return uriString val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) cursor.moveToFirst() val name = cursor.getString(nameIndex) cursor.close() try { val file = File.createTempFile("temp", name, reactApplicationContext.cacheDir) file.deleteOnExit() val stream = reactApplicationContext.contentResolver.openInputStream(uri) return if (stream != null) { val source = stream.source().buffer() val sink = file.sink().buffer() sink.writeAll(source) sink.close() file.absolutePath } else { uriString } } catch (e: FileNotFoundException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } return uriString }}