#android
Вопрос:
Как startActivityForResult
было объявлено устаревшим, я понял , как заменить его пользователем Contract
, который должен быть объявлен ранее OnCreate
, поэтому я создал следующее:
class MainActivity : AppCompatActivity() {
companion object {
init {
System.loadLibrary("native-lib")
}
}
val cameraResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
result.data?.let {
// if (it.hasExtra("resultTest")) {
try {
photoFile = createImageFile()
// Continue only if the File was successfully created
if (photoFile != null) {
val photoURI = FileProvider.getUriForFile(
ctx,
"mypackage.provider",
photoFile!!
)
Toast.makeText(ctx, photoURI.toString(), Toast.LENGTH_SHORT).show()
Log.e(TAG, photoURI.toString())
}
} catch (ex: Exception) {
// Error occurred while creating the File
displayMessage(baseContext, ex.message.toString())
}
}
}
}
Но все вышеперечисленное было создано для того , чтобы прислушиваться к одному намерению, как:
var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
contentSelectionIntent.type = "image/*"
if (ContextCompat.checkSelfPermission(
ctx,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
0
)
}
cameraResult.launch(takePictureIntent)
Все вышесказанное соответствует camera
намерению.
Вопрос в том, как с этим справиться, если у меня несколько намерений, как в приведенном ниже коде:
val intentArray: Array<Intent?> = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(0)
val chooserIntent = Intent(Intent.ACTION_CHOOSER)
chooserIntent.let {
it.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
it.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
it.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
cameraResult.launch(it) // < --- Here
startActivity(it)
}
Then how can I listen to the responces of these different intents?
Hint When working with OnActivityResult
there was requestCode
to help, but here there is nothng like it.
I tried to launch
them for each intent
seperatly, but found that put intents are launched, while I need the used to select one only, then the related listner to be launched only:
var takePictureIntent: Intent? = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePictureIntent.let {
// cameraResult.launch(it)
}
val intentArray: Array<Intent?> = takePictureIntent?.let { arrayOf(it) } ?: arrayOfNulls(0)
val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
contentSelectionIntent.let{
it.addCategory(Intent.CATEGORY_OPENABLE)
it.type = "image/*"
// pickResult.launch(it)
}
val chooserIntent = Intent(Intent.ACTION_CHOOSER)
chooserIntent.let {
it.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
it.putExtra(Intent.EXTRA_TITLE, "Image Chooser")
it.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
startActivity(it)
}