0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-19 19:42:18 +02:00

Allow file input from share menu

Updates the handleSendText method to support both types of intents.

Also extends the accepted file types to text/* to also accept VCards
and other text-based files.
This commit is contained in:
ThetaDev 2024-06-30 16:54:20 +00:00 committed by GitHub
parent be6539d41f
commit 24730dfde3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

View File

@ -62,7 +62,7 @@
<intent-filter android:label="@string/compose_barcode">
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="text/*"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>

View File

@ -52,6 +52,8 @@ import de.markusfisch.android.zxingcpp.ZxingCpp.BarcodeFormat
import de.markusfisch.android.zxingcpp.ZxingCpp.Binarizer
import de.markusfisch.android.zxingcpp.ZxingCpp.ReaderOptions
import de.markusfisch.android.zxingcpp.ZxingCpp.Result
import java.io.FileInputStream
import java.util.Scanner
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
@ -135,7 +137,7 @@ class CameraActivity : AppCompatActivity() {
initDetectorView()
if (intent?.action == Intent.ACTION_SEND &&
intent.type == "text/plain"
intent.type?.startsWith("text/") == true
) {
handleSendText(intent)
}
@ -347,9 +349,27 @@ class CameraActivity : AppCompatActivity() {
private fun handleSendText(intent: Intent) {
val text = intent.getStringExtra(Intent.EXTRA_TEXT)
if (text?.isEmpty() == false) {
startActivity(MainActivity.getEncodeIntent(this, text, true))
finish()
return
}
// Read text from file
val textUri = intent.getParcelableExtra(Intent.EXTRA_STREAM) as Uri?
if (textUri != null) {
val file = contentResolver.openFileDescriptor(textUri, "r")
if (file != null) {
val fs = FileInputStream(file.fileDescriptor)
val scn = Scanner(fs).useDelimiter("\\A")
if (scn.hasNext()) {
val fText = scn.next()
startActivity(MainActivity.getEncodeIntent(this, fText, true))
finish()
}
file.close()
}
}
}