Error when selecting 1 image from Intent Multiple - android

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()
}
}

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

How to upload multiple files in WebView on Android in Kotlin?

I have tried to use the WebView to upload one single file and it works well. However, once I selected more than 1 file and upload, the program crashed. The code is below:
override fun onActivityResult(
requestCode: Int, resultCode: Int,
intent: Intent?
) {
super.onActivityResult(requestCode, resultCode, intent)
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage || requestCode != FILECHOOSER_RESULTCODE) return
var results: Array<Uri>? = null
if (resultCode === Activity.RESULT_OK) {
if (intent != null) {
val dataString = intent.dataString
val clipData = intent.clipData
if (clipData != null) {
for (i in 0 until clipData.itemCount) {
val item = clipData.getItemAt(i)
results!![i] = item.uri //Here is the crash point
}
}
if (dataString != null) results =
arrayOf(Uri.parse(dataString))
}
}
mUploadMessage!!.onReceiveValue(results)
mUploadMessage = null
return
}
}
And here is the code in WebChromeClient():
override fun onShowFileChooser(
view: WebView,
filePathCallback: ValueCallback<Array<Uri>>,
fileChooserParams: FileChooserParams
): Boolean {
if (mUploadMessage!= null) {
mUploadMessage!!.onReceiveValue(null);
mUploadMessage = null;
}
mUploadMessage = filePathCallback
val intent = fileChooserParams.createIntent()
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.setType("*/*")
startActivityForResult(intent, FILECHOOSER_RESULTCODE)
return true
}
From the logcat I can see the crash point is results!![i] = item.uri when I get the uri from clipData in the For loop in OnActivityResult. I have comment on that code line. The crash message is "kotlin.KotlinNullPointerException".
I found the solution.
Just need to initialize the results:
results= Array(clipData.itemCount, {clipData.getItemAt(0).uri})

Cannot see any image in my emulator in Kotlin

I am trying to select an image in an activity but as soon as I click to select, I get to the Device Folder but it has no picture in it. Please see my code below:
class AddPainting : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_painting)
}
fun select(view: View) {
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 2);
} else {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if(requestCode == 2) {
if(grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
val intent = Intent(Intent.ACTION_PICK);
intent.type = "image/*";
startActivityForResult(intent, 1);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == 1 && requestCode == Activity.RESULT_OK && data != null) {
val image = data.data;
try{
val selectedImage = MediaStore.Images.Media.getBitmap(this.contentResolver, image);
imageView.setImageURI(data?.data);
}catch (e: Exception) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data)
}
fun save(view: View) {
}
}
This is what I see when it transfers me to pick a picture
Use your selectedImage bitmap to set in ImageView
imageView.setImageURI(data?.data);
to
imageView.setImageURI(selectedImage);

Android activity startActivityForResult not being called

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.

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