Android activity startActivityForResult not being called - android

I am trying to get a file and pass that file to Firebase and store it in my storage bucket but for some reason my startActivityForResult isn't being called even after adding the attribute android:launchMode="singleTop" to the manifest file as suggested in another answer and also not using the kotlin static function call on my button click event that is supposed to start the intent. And note both are in the same activity!
Here is my button click handler:
override fun onCreate(savedInstanceState: Bundle?) {
btnImage.setOnClickListener {
val intent = Intent()
intent.action = Intent.ACTION_GET_CONTENT
// set the intent type
intent.type = "image/*"
// accept only local content
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
startActivityForResult(Intent.createChooser(intent, "Insert Picture"), PICTURE_RESULT)
}
}
And this is my startAtivityForResult function:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// super.onActivityResult(requestCode, resultCode, data)
if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK) {
if (data != null) {
val imageUri : Uri = data.data!!
val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
ref.putFile(imageUri)
{
} else {
Log.d("IMAGE", resultCode.toString())
Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
}
}
}

Android activity startActivityForResult not being called
You need to call super.onActivityResult(requestCode, resultCode, data) inside onActivityResult
You have commented the code of your super.onActivityResult(requestCode, resultCode, data) inside onActivityResult
Remove // from your super.onActivityResult(requestCode, resultCode, data) inside onActivityResult
Also, Use this
if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT)
insread of this
if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK)
SAMPLE CODE
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT) {
if (data != null) {
val imageUri : Uri = data.data!!
val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
ref.putFile(imageUri)
{
} else {
Log.d("IMAGE", resultCode.toString())
Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
}
}
}

Change resultCode == PICTURE_RESULT to:
requestCode == PICTURE_RESULT in your if statement.

Related

I want to use the data i got in onActivityResult to oncreate after taking a selfie

This sends user to camera:
binding.btnTakeSelfie.setOnClickListener {
val takeSelfie = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takeSelfie.resolveActivity(this.packageManager)!=null){
startActivityForResult(takeSelfie, REQUEST_CODE)
} else {
Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE && data != null) {
val images:Bitmap = data.extras!!.get("data") as Bitmap
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setImageBitmap(images)
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
Now I want to use images in my oncreate after it returns from taking a picture

Error when selecting 1 image from Intent Multiple

I have a code below that works fine when selecting multiple image however if the image selected is only one it gave me an error. Here is the error
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1110896904, result=-1, data=Intent
And here is the code
#SuppressLint("IntentReset")
private fun checkAccessAndUpload() {
if (context?.let {
EasyPermissions.hasPermissions(
it,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
} == true) {
if (Build.VERSION.SDK_INT < 19) {
var intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(intent, "Select Picture")
, PICK_IMAGE_MULTIPLE
)
} else {
var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, PICK_IMAGE_MULTIPLE)
}
} else {
EasyPermissions.requestPermissions(
this,
"We need to access your camera and storage to upload your pictures",
123,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
uploadImage()
}
}
The error happens here
val count = data.clipData!!.itemCount
How can I fix this error so my gallery intent can select 1 or multple
Change code like this
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
if(data.clipData != null) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
}else if(data.data != null) {
var imageUri: Uri = data.data!!
ImageList.add(imageUri)
}
uploadImage()
}
}

take photo in camera and send file to server in android(10 and 11)

I want to send a photo from the gallery and the camera as a file to the server and I have used the library canhub but in the activityResult the data is null.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
GALLERY_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
launchImageCrop(uri)
}
}
}
CAMERA_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
data!!.data?.let { uri ->
launchImageCrop(uri)
}
}
}
CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE -> {
val result = CropImage.getActivityResult(data)
if (resultCode == Activity.RESULT_OK && requestCode == GALLERY_REQUEST_CODE) {
val file = File(Environment.getExternalStorageDirectory(), "read.me")
val uri = Uri.fromFile(file)
val auxFile = File(uri.path)
viewModel.getUserCardInfo(auxFile)
} else {
(resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
// val error = CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE
// toast(error)
}
if (resultCode == Activity.RESULT_OK && requestCode == CAMERA_REQUEST_CODE) {
val photo: Bitmap = result!!.bitmap
val mFile = context?.convertToFile(photo)
if (mFile != null) {
viewModel.getUserCardInfo(mFile)
}
}else{
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE
}
}
}
}
and
private fun launchImageCrop(uri: Uri) {
CropImage.activity(uri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1920, 1080)
.setCropShape(CropImageView.CropShape.RECTANGLE)
.start(requireContext(),this);
}
and add manifest
android:requestLegacyExternalStorage="true"
Hi i faced a similar issue , please try this sometimes the problem is google are not allowing to use Environment.getExternalStorageDirectory()
This is the new way
ContextWrapper cw = new ContextWrapper(mContext);
File directory = cw.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(directory, "name_of_file");
Also please don't use android:requestLegacyExternalStorage="true", google might reject your app in playstore

How to get original requestcode from onActivityResult which is call another onActivityresult in itself?

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
data?.data?.let { cropImage(it) }
if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
try {
val resultdata = CropImage.getActivityResult(data)
...
if (requestCode == IMAGE1) {
...
} else if (requestCode == IMAGE2) {
...
}
}
...
}
}
The CropImage method return another requestCode and override it so I can't use the function which is use IMAGE1, IMAGE2.
How can i use request==IMAGE1,2 Function?
I suppose you use CropImage library.
You can use it like following also.
Intent intent = CropImage.activity(uri)
.setGuidelines(CropImageView.Guidelines.ON)
.setCropShape(CropImageView.CropShape.RECTANGLE)
.setMultiTouchEnabled(true)
.getIntent(getActivity());
startActivityForResult(intent, IMAGE1);

While using the camera or gallery in app, it crashes on backButton press

I am trying to use the camera or gallery (as an alert) in an application for uploading documents. The app crashes if the back button is pressed and no image was selected.
private fun getImageFromCamera() {
val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePicture.resolveActivity(packageManager) != null) {
startActivityForResult(takePicture, TAKE_PICTURE)
}
}
private fun getImageFromGallery() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(Intent.createChooser(intent, "Select
Picture"), PICK_IMAGE)
}
}
There is no issue regarding the permission for the camera. I didn't post all the code.
onActivityResult:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
try {
val inputStream1 = contentResolver.openInputStream(data.data!!)
bitmap = BitmapFactory.decodeStream(inputStream1)
mUploadPanCardImageButton!!.setImageBitmap(bitmap)
mUploadPanCardTextView?.visibility = View.GONE
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}else if (requestCode == TAKE_PICTURE) {
if (resultCode == Activity.RESULT_OK) {
val extras1 = data.extras
bitmap = extras1!!.get("data") as Bitmap
mUploadPanCardImageButton!!.setImageBitmap(bitmap)
mUploadPanCardTextView?.visibility = View.GONE
}
}
}
Here is the Exception that I am getting:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=12345, result=0, data=null} to activity {in.inspiringwave.firstapp/in.inspiringwave.firstapp.UploadDocsActivity}:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data at
android.app.ActivityThread.deliverResults(ActivityThread.java:4173) at
android.app.ActivityThread.handleSendResult(ActivityThread.java:4216) at
android.app.ActivityThread.-wrap20(ActivityThread.java)
Make your onActivityResult Nullable by putting ? for intent like this-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

Categories

Resources