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);
Related
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
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()
}
}
I am using image picker inside recycler view using this code
adapter
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvAnimalType.text = items[position].name
holder.image.setOnClickListener{
requestPermissions(it.context as Activity , arrayOf(WRITE_EXTERNAL_STORAGE),1)
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
(it.context as Activity).startActivityForResult(intent, 1)
Log.i(uri.toString(),"hello")
}
}
ParentActivity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Log.d("selectedImageUri()","hello")
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
1 -> {/*file front*/
if (resultCode == Activity.RESULT_OK && data != null) {
val selectedImageUri = data.data as Uri
val selectedImageBitmap: Bitmap =
MediaStore.Images.Media.getBitmap(this.contentResolver, selectedImageUri)
adapter!!.uri(selectedImageUri)
Log.d(selectedImageUri.toString(),"hello")
}
}
}
}
My problem is how to know when image is selected by the user as the gallery activty runs in async.
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) }
//
}
}
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?) {