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

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

Related

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

Unable to capture image in android 11

i'm trying to capture image in android 11 device but request is keep cancelled. i have added also. still unable to capture image. pick image from gallery is working. image is not storing in Android/data folder.
i have added val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) also still unable to create temp file in storage. please provide any solutions
private fun selectImage() {
imageUtils.createImageFile(applicationContext).let {
viewModel.setFileTempPath(it.absolutePath, "doc_name")
startActivityForResult(imageUtils.captureImage(applicationContext, it), 300)
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
300 -> when (resultCode) {
RESULT_OK -> {
Log.d("tmppath", data?.data.toString())
if (data != null && data.data != null) {
val selectedImage: Bitmap =
imageUtils.getBitmap(data.data, this#TaggingActivity)!!
viewModel.dataModel.selectedImagesArrayList.value.let {
if (it == null) {
viewModel.dataModel.selectedImagesArrayList.value = ArrayList()
}
val a = it
a?.add(
ImageDetailsToUpload(
"",
selectedImage,
Constant.CUSTOMER_GEO_TAG,
viewModel.dataModel.documentName.value,
viewModel.commonModel.currentLocation.value!!
)
)
viewModel.dataModel.selectedImagesArrayList.postValue(a)
}
} else {
if (viewModel.dataModel.tmpPath.value != null) {
imageUtils.getBitmapFromPath(viewModel.dataModel.tmpPath.value)
?.let { selectedImage ->
val a = viewModel.dataModel.selectedImagesArrayList.value
a?.add(
ImageDetailsToUpload(
"",
selectedImage,
Constant.CUSTOMER_GEO_TAG,
viewModel.dataModel.documentName.value,
viewModel.commonModel.currentLocation.value!!
)
)
viewModel.dataModel.selectedImagesArrayList.postValue(a)
} ?: run {
viewModel.commonModel.showToastTextView("Sorry Try Again..!")
}
} else {
viewModel.commonModel.showToastTextView("Sorry Try Again..!")
return
}
}
viewModel.dataModel.tmpPath.value = null
}
RESULT_CANCELED -> {
viewModel.setFileTempPath("", "")
Toast.makeText(this,"cancelled",Toast.LENGTH_LONG).show()
}
}
}
//Intent class
fun getCameraIntent(file: File): Intent {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file))
if (Build.VERSION.SDK_INT >= 24) {
try {
val m =
StrictMode::class.java.getMethod("disableDeathOnFileUriExposure")
m.invoke(null)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
} else {
takePictureIntent.putExtra("return-data", true)
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val pickPhoto = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
val intents: ArrayList<Intent> = arrayListOf()
intents.add(takePictureIntent)
intents.add(pickPhoto)
val chooserIntent = Intent.createChooser(
intents.removeAt(intents.size - 1),
" Document Upload"
)
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(arrayOf<Parcelable>()))
return chooserIntent
}

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

kotlin.KotlinNullPointerException File not founded, only in samsung devices

I used this method to get file from uri, it is work fine with all devices but only crash with samsung devices!
fun Activity.getFilePath(uri: Uri): File {
var cursor: Cursor? = null
return try {
val dataArray = arrayOf(MediaStore.Images.Media.DATA)
cursor = contentResolver.query(uri, dataArray, null, null, null)
val index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor?.moveToFirst()
File(index?.let { cursor?.getString(it) })
} catch (e: Exception) {
throw KotlinNullPointerException("File not founded")
} finally {
cursor?.close()
}
}
get image from gallery
fun Activity.chooseImageFromGallery(withAskPermission: Boolean) {
if (isPermissionsGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
val intent = Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST)
} else {
if (withAskPermission && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
PICK_IMAGE_REQUEST)
}
}
}
on Activity result
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
uri = data?.data
file = uri?.let { activity?.getFilePath(it) }
//
}
}

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