I started screen capture intent according to MediaProjectionManager:
var intent = mediaProjectionManager.createScreenCaptureIntent()
startActivityForResult(intent, 0)
... but I have no idea how to stop screen capture. Sample projects for API 21 are still not available.
ok, found solution, firstly save result to MediaProjection instance:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super<Activity>.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data)
}
}
and to stop simply execute:
mediaProjection.stop()
Related
I am trying to get data from one activity in other. But data always null.
val res = Intent()
res.putExtra("uri", imageUri)
setResult(Activity.RESULT_OK, res)
finish()
In the second activity I always get data as null. What is the problem? (imageUri is not null when I put it).
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == Activity.RESULT_OK){
//data == null, WHY???
}
}
I think you need to define the intent with the current context and target class.
val intent = Intent(this#HomeActivity,ProfileActivity::class.java) // define you activity in which you want to go
intent.putExtra("Username","John Doe")
startActivity(intent)
Although everything work as intended and I get results from the Places API, the onActivityResult callback is never called when the user selects a result. What happens is that the result appears in the search textview and the Autocomplete activity doesn't exit. I have to press back for it to close.
// Set the fields to specify which types of place data to return.
val fields = Arrays.asList(Place.Field.ID, Place.Field.NAME)
// Start the autocomplete intent.
val intent: Intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields
).setTypeFilter(TypeFilter.ADDRESS).
setCountry("DE")
.build(requireContext())
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(data!=null) {
var status: Status = getStatusFromIntent(data)
//var place: Place = getPlaceFromIntent(data)
}
super.onActivityResult(requestCode, resultCode, data)
}
Do you run method startActivityForResult inside Fragment? Check this out: onActivityResult is not being called in Fragment
You have to override onActivityResult in Activity which contains your fragment.
// In your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
This will call your fragment's onActivityResult()
Ensure you're using the latest version of Place client SDK Places 2.1.0
. I don't see any reason in your code that causes the behavior you've addressed. Just use the below implementation and try.
private fun pickNewPlace() {
if (!Places.isInitialized()) {
Places.initialize(context!!, getString(R.string.google_service_api_key))
}
// Set the fields to specify which types of place data to return.
val fields = listOf(Place.Field.ID, Place.Field.NAME)
// Start the autocomplete intent.
val intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields
).setTypeFilter(TypeFilter.ADDRESS).setCountry("DE").build(context!!)
startActivityForResult(intent, REQUEST_CODE_DEFAULT)
}
onActivityResult result handling
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_DEFAULT) {
if (resultCode == RESULT_OK) {
// Get relevent info from place object
val place = Autocomplete.getPlaceFromIntent(data!!)
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
val status = Autocomplete.getStatusFromIntent(data!!)
showAlertMessage(status.statusMessage ?: getString(R.string.something_went_wrong))
}
}
}
I am using intents to launch the camera app on Android. The problem is on Samsung Note 9 I am seeing that the preview images is rotated upside down. However the bitmap obtained in onActivityResult is fine.
I want to fix the preview image screen using the Intent approach.
private fun dispatchTakePictureIntent() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
ivTest.setImageBitmap(imageBitmap)
}
}
Sorry, but you have no means of controlling the preview behavior of a third-party camera app.
My Code works well in API 28 and below except API 19.
the main problem is that onActivityResult doesn't call from other activity in my code when i run it in a device with API 19
The min_sdk is 17. compile_sdk && target_sdk are 27, so the problem will not solve with changing them.
here is the code from the first Activity:
food.name = name
intent.putExtra(Food_Name, burgerNameEtxt.text.toString())
setResult(Activity.RESULT_OK, intent)
finish()
and this is where i should receive the result in another Activity:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
RequestCode -> {
when (resultCode) {
Activity.RESULT_OK -> {
var food = getCurrentFood(mMenueItem.id)
food.name = data!!.getStringExtra(Food_Name)
// doing some thing with data //
}
}
}
}
}
the RequestCode is 1 >> const val RequestCode = 1
which i send it to the first activity with intent. any help will be appreciated :)
For me, it happens because of android:launchMode="singleTask" and this doesn't help intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).
I fixed it by android:launchMode="singleTop".
I'm want to do something like whatsapp, when user capture an image or select an image from gallery, they can edit the image/ draw something on the image.
Any help will be appreciated.
There are so many libraries for that.
Just try this Whatsapp like photo editor
Checkout the sample and library
ImageEditor.Builder(this, imagePath)
.setStickerAssets("stickers")
.disable(ImageEditor.EDITOR_TEXT) //to disable something
.open()
You will get the result in onActivityResult
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
ImageEditor.RC_IMAGE_EDITOR ->
if (resultCode == Activity.RESULT_OK && data != null) {
val imagePath: String = data.getStringExtra(ImageEditor.EXTRA_EDITED_PATH)
edited_image.setImageBitmap(BitmapFactory.decodeFile(imagePath))
}
}
}