'OnActivityResult given null data from intent
Im getting problem when I get image from gallery and put intent data as null. I have same code in other fragment where I register a user with foto and there is no problem there. This is profilefragment where the error happens
private fun loadImage() {
/*val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, pickImage)*/
val imageIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
startActivityForResult(imageIntent, pickImage) //funciona*/
}
@Suppress("DEPRECATION")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == AppCompatActivity.RESULT_OK && requestCode == pickImage) {
if (data != null) {
try {
val imageUri = data.data //data.extras!!.get("data") as Bitmap
val bitmap =
MediaStore.Images.Media.getBitmap(context?.contentResolver, imageUri)
avatar.setImageURI(imageUri)
register_avatarFilename.setText("image_test")
register_avatarFiledata.text = saveImage(bitmap)
} catch (ioe: IOException) {
ioe.printStackTrace()
register_avatarFilename.setText("Image upload failed")
}
}
}
}
fun saveImage(myBitmap: Bitmap): String {
val bytes = ByteArrayOutputStream()
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes)
val b = bytes.toByteArray()
val encodedAvatar = Base64.encodeToString(b, Base64.DEFAULT)
return encodedAvatar
}
Solution 1:[1]
Put this code In Activity One
Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();
And this code on second activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && requestCode==1) {
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle.getString("Date");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parveen Haade |
