how to Upload Image from fragment to firebase using activity Result? (Kotlin) - android

Trying to upload an image to firebase from fragment. I also start the activity by using this command: activity!!.startActivityForResult......
But still the same error.
Libraries I used to crop image and compress the image:
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
implementation 'id.zelory:compressor:2.1.0'
changeProfile is a button
val GALLERY_ID:Int = 1
val changeProfile = view.findViewById<Button>(R.id.changeProfileImage)
changeProfile.setOnClickListener{
var galleryIntent = Intent().apply {
type = "image/*"
action = Intent.ACTION_GET_CONTENT
}
startActivityForResult(Intent.createChooser(galleryIntent,
"SELECT_IMAGE"), GALLERY_ID)
}
Heres my activity result where I first check the error in the activity result. The I upload the croped image to firebase storage as a profile image.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == GALLERY_ID
&& resultCode == Activity.RESULT_OK) {
var image: Uri = data!!.data
var userId = mCurrentUser!!.uid
CropImage.activity(image)
.setAspectRatio(1, 1)
.start(context as Activity,this)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result = CropImage.getActivityResult(data)
if (resultCode == Activity.RESULT_OK) {
val resultUri = result.uri
var thumbFile = File(resultUri.path)
var thumbBitmap = Compressor(activity)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(65)
.compressToBitmap(thumbFile)
//We upload images to firebase
var byteArray = ByteArrayOutputStream()
thumbBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
byteArray)
var thumbByteArray: ByteArray
thumbByteArray = byteArray.toByteArray()
var filePath = mStorageRef!!.child("chat_profile_images")
.child(userId + ".jpg")
//Create another directory for thumbimages ( smaller, compressed images)
var thumbFilePath = mStorageRef!!.child("chat_profile_images")
.child("thumbs")
.child(userId + ".jpg")
filePath.putFile(resultUri)
.addOnCompleteListener{
task: Task<UploadTask.TaskSnapshot> ->
if (task.isSuccessful) {
//Let's get the pic url
var donwloadUrl = task.result.downloadUrl.toString()
//Upload Task
var uploadTask: UploadTask = thumbFilePath
.putBytes(thumbByteArray)
uploadTask.addOnCompleteListener{
task: Task<UploadTask.TaskSnapshot> ->
var thumbUrl = task.result.downloadUrl.toString()
if (task.isSuccessful) {
var updateObj = HashMap<String, Any>()
updateObj.put("image", donwloadUrl)
updateObj.put("thumb_image", thumbUrl)
//We save the profile image
mDatabase!!.updateChildren(updateObj)
.addOnCompleteListener {
task: Task<Void> ->
if (task.isSuccessful) {
Toast.makeText(activity, "Profile Image Saved!",
Toast.LENGTH_LONG)
.show()
}else {
}
}
}else {
}
}
}
}
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
val error = result.error
Log.e("Error", error.toString())
}
}
}
else {
Toast.makeText(activity,"Activity Result Error", Toast.LENGTH_LONG).show()
}
}

Related

Firebase image upload failing

I am trying to upload an image for an user when he creates an account but account is being created but the image isn't getting uploaded,not even the folder images/ is being created in the firebase.I have tried several methods to upload an image but nothing helps .Sharing the code below :
class RegistrationActivity : AppCompatActivity() {
lateinit var auth: FirebaseAuth
private lateinit var picBtn : ImageView
private var filePath: Uri? = null
private val PICK_IMAGE_REQUEST = 22
var storage: FirebaseStorage? = null
var storageReference: StorageReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
getSupportActionBar()?.hide()
val signupBtn = findViewById<Button>(R.id.signup_btn)
auth = Firebase.auth
picBtn = findViewById(R.id.pro_pick_btn)
//Profile Picture Selection
picBtn.setOnClickListener{
SelectImage()
}
//Signup Procedure
signupBtn.setOnClickListener {
//Hide Keyboard and Button
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(it.windowToken, 0)
var fname = findViewById<EditText>(R.id.f_name).text.toString().trim()
var lname = findViewById<EditText>(R.id.l_name).text.toString().trim()
var email = findViewById<EditText>(R.id.reg_email).text.toString().trim()
var pswd = findViewById<EditText>(R.id.reg_pswd).text.toString().trim()
var cpswd = findViewById<EditText>(R.id.reg_cpswd).text.toString().trim()
//Full Auth Process
if ((pswd == cpswd) && (fname != "") && (lname != "") && (pswd != "") && (email != "")){
signupBtn.setVisibility(View.INVISIBLE)
//Progress Bar
val transactionProgress = supportFragmentManager.beginTransaction()
transactionProgress.setTransition(androidx.fragment.app.FragmentTransaction.TRANSIT_NONE)
transactionProgress.add(R.id.progressContainer, ProgressView(), "Progress Bar").commit()
//Authentication Process
auth.createUserWithEmailAndPassword(email, pswd)
.addOnCompleteListener(this#RegistrationActivity) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success")
val user = auth.currentUser
uploadImage()
updateUI(user)
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.exception)
Toast.makeText(
baseContext, "Authentication failed.",
Toast.LENGTH_SHORT
).show()
updateUI(null)
}
}
}
else if( (pswd != cpswd) && (fname != "") && (lname != "") && (pswd != "") && (email != "")){
Toast.makeText(this#RegistrationActivity, "Please enter a matching Password", Toast.LENGTH_LONG).show() }
else
Toast.makeText(this#RegistrationActivity, "Enter all fields", Toast.LENGTH_LONG).show()
}
}
private fun uploadImage() {
if (filePath != null) {
// Code for showing progressDialog while uploading
val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Uploading...")
progressDialog.show()
// Defining the child of storageReference
val ref: StorageReference? = storageReference?.child(
"images/"
+ UUID.randomUUID().toString()
)
// adding listeners on upload
// or failure of image
ref?.putFile(filePath!!)
?.addOnSuccessListener { // Image uploaded successfully
// Dismiss dialog
progressDialog.dismiss()
Toast
.makeText(
this#RegistrationActivity,
"Image Uploaded!!",
Toast.LENGTH_SHORT
)
.show()
Log.d("Anandhu:","Good going")
}
?.addOnFailureListener { e -> // Error, Image not uploaded
progressDialog.dismiss()
Toast
.makeText(
this#RegistrationActivity,
"Failed " + e.message,
Toast.LENGTH_SHORT
)
.show()
Log.d("Anandhu:","Bad going")
}
?.addOnProgressListener { taskSnapshot ->
// Progress Listener for loading
// percentage on the dialog box
val progress = (100.0
* taskSnapshot.bytesTransferred
/ taskSnapshot.totalByteCount)
progressDialog.setMessage(
"Uploaded "
+ progress.toInt() + "%"
)
Log.d("Anandhu:"," going")
}
}
}
private fun SelectImage() {
// Defining Implicit Intent to mobile gallery
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(
intent,
"Select Image from here..."
),
PICK_IMAGE_REQUEST
)
}
// Override onActivityResult method
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(
requestCode,
resultCode,
data
)
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.data != null) {
// Get the Uri of data
filePath = data.data
try {
// Setting image on image view using Bitmap
val bitmap = MediaStore.Images.Media
.getBitmap(
contentResolver,
filePath
)
val bitmapDrawable = BitmapDrawable(bitmap)
picBtn.setImageDrawable(bitmapDrawable)
} catch (e: IOException) {
// Log the exception
e.printStackTrace()
}
}
}
//Forward Function
private fun updateUI(user: FirebaseUser?) {
val loginIntent = Intent(this#RegistrationActivity,LoginActivity::class.java)
startActivity(loginIntent)
finish()
}
}

Adding user profile picture in Kotlin

I've recently started programming in Kotlin and cannot seem to add a profile picture to a user when registering it.
According to the code here, I can access to the gallery and retrieve the image information. The picture will appear on screen, but after registering the user the image url will not appear anywhere.
class RegisterUser : AppCompatActivity() {
private val database = FirebaseDatabase.getInstance()
private val auth: FirebaseAuth = FirebaseAuth.getInstance()
private val UserCreation = database.getReference("Usuarios")
private val pickImage = 100
private var imageUri: Uri? = null
lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register_user)
Goback.setOnClickListener {
val Gobackou = Intent(this, MainActivity::class.java)
startActivity(Gobackou)
}
RegisterConfirm.setOnClickListener {
val SetUser = SetUser.text.toString()
val SetPass = setPass.text.toString()
val SetEmail = SetEmail.text.toString()
if (SetUser.isEmpty() && SetPass.isEmpty() && SetEmail.isEmpty()) {
Toast.makeText(this, "Faltan Campos", Toast.LENGTH_SHORT).show()
} else {
RegisterUserv2(SetEmail, SetPass, SetUser)
}
}
selectPP.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, pickImage)
}
}
var selectedPhotoUri: Uri? = null
//guardar la foto de perfil
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == pickImage) {
val selectedPhotoUri = data?.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
val bitmapDrawable = BitmapDrawable(bitmap)
userimg.setBackgroundDrawable(bitmapDrawable)
}
}
private fun RegisterUserv2(email: String, password: String, user: String) {
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
uploadimage()
UltrasaveUsuario(Usuarios(auth.currentUser!!.uid, user, password, email))
val Gobackou = Intent(this, MainActivity::class.java)
startActivity(Gobackou)
} else {
Toast.makeText(this, "Registro ERROR", Toast.LENGTH_LONG).show()
}
}
}
private fun UltrasaveUsuario(usuario: Usuarios) {
val mensajeFirebase = UserCreation.push()
usuario.id = mensajeFirebase.key ?: ""
mensajeFirebase.setValue(usuario)
}
private fun uploadimage(imageurl: String){
if (selectedPhotoUri == null) return
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/images/$filename/")
ref.putFile(selectedPhotoUri!!)
.addOnSuccessListener {
ref.downloadUrl.addOnSuccessListener{
}
}
}
}
Since you are using firebase I will show you how to save firebase storage.
private fun uploadImage() {
if (mSelectedImageFileUri != null) {
val sRef: StorageReference = FirebaseStorage.getInstance().reference.child(
"users/ $uid/profile.jpg"
)
sRef.putFile(mSelectedImageFileUri!!)
.addOnSuccessListener { taskSnapshot ->
taskSnapshot.metadata!!.reference!!.downloadUrl
.addOnSuccessListener { url ->
}
}.addOnFailureListener {
//error
}
} else {
}
}
then you will take this picture using the user id to take .
firebase>storage to view the image
getImage
val sRef: StorageReference = FirebaseStorage.getInstance().reference.child(
"users/ $uid/profile.jpg"
)
sRef.downloadUrl.addOnSuccessListener {
Picasso.get().load(it).into(globalprofileImage)
}
Build.gradle
implementation 'com.squareup.picasso:picasso:2.71828'
https://www.youtube.com/watch?v=nNYLQcmB7AU&t=449s&ab_channel=SmallAcademy

Android Kotlin: if i don't select images to save on the app, the app do not work

On my app, users can save some notes with image. But, users can not save their notes without image. Thus image is compulsory to save their data. But ı want to change it. I want to allow them to save their note without image. How can ı do this ? This is my saveclcikbutton codes;
class TakeNotesActivity : AppCompatActivity() {
var selectedPicture: Uri? = null
var selectedBitmap: Bitmap? = null
private lateinit var db: FirebaseFirestore
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_take_notes)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
}
fun putPictureClick(view: View) {
val popupMenu = PopupMenu(this, view)
val inflater = popupMenu.menuInflater
inflater.inflate(R.menu.secondmenu, popupMenu.menu)
popupMenu.show()
popupMenu.setOnMenuItemClickListener {
if (it.itemId == R.id.galleryImage) {
if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
1
)
} else {
val intentToGallery =
Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intentToGallery, 2)
}
}
if (it.itemId == R.id.capturePhoto) {
if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.CAMERA),
10
)
} else {
openCamera()
}
}
if (it.itemId == R.id.cancel) {
Toast.makeText(applicationContext, "Canceled", Toast.LENGTH_LONG).show()
}
true
}
}
fun openCamera() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
selectedPicture =
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
// Camera Intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, selectedPicture)
startActivityForResult(cameraIntent, 20)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == 1) {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
val intentToGallery =
Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intentToGallery, 2)
}
}
if (requestCode == 10) {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera()
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show()
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 2 && resultCode == Activity.RESULT_OK && data != null) {
selectedPicture = data.data
try {
if (selectedPicture != null) {
if (Build.VERSION.SDK_INT >= 28) {
val source =
ImageDecoder.createSource(this.contentResolver, selectedPicture!!)
selectedBitmap = ImageDecoder.decodeBitmap(source)
imageButton.setImageBitmap(selectedBitmap)
} else {
selectedBitmap =
MediaStore.Images.Media.getBitmap(this.contentResolver, selectedPicture)
imageButton.setImageBitmap(selectedBitmap)
}
}
} catch (e: Exception) {
}
}
//called when image was captured from camera intent
if (requestCode == 20 && resultCode == Activity.RESULT_OK && data != null) {
imageButton.setImageURI(selectedPicture)
}
super.onActivityResult(requestCode, resultCode, data)
}
fun saveClick(view: View) {
//UUID -> Image Name
val uuid = UUID.randomUUID()
val imageName = "$uuid.jpg"
val storage = FirebaseStorage.getInstance()
val reference = storage.reference
val imagesReference = reference.child("images").child(imageName)
if (selectedPicture != null) {
imagesReference.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->
// take the picture link to save the database
val uploadedPictureReference =
FirebaseStorage.getInstance().reference.child("images").child(imageName)
uploadedPictureReference.downloadUrl.addOnSuccessListener { uri ->
val downloadUrl = uri.toString()
println(downloadUrl)
val noteMap = hashMapOf<String, Any>()
noteMap.put("downloadUrl", downloadUrl)
noteMap.put("userEmail", auth.currentUser!!.email.toString())
noteMap.put("noteTitle", titleText.text.toString())
noteMap.put("yourNote", noteText.text.toString())
noteMap.put("date", Timestamp.now())
db.collection("Notes").add(noteMap).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(
applicationContext,
exception.localizedMessage?.toString(),
Toast.LENGTH_LONG
).show()
}
}
}
}
}
}
I also try to add on saveClick fun the codes below but did not work. What should I do ?
fun saveClick(view: View) {
//UUID -> Image Name
val uuid = UUID.randomUUID()
val imageName = "$uuid.jpg"
val storage = FirebaseStorage.getInstance()
val reference = storage.reference
val imagesReference = reference.child("images").child(imageName)
if (selectedPicture != null) {
imagesReference.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->
// take the picture link to save the database
val uploadedPictureReference =
FirebaseStorage.getInstance().reference.child("images").child(imageName)
uploadedPictureReference.downloadUrl.addOnSuccessListener { uri ->
val downloadUrl = uri.toString()
println(downloadUrl)
val noteMap = hashMapOf<String, Any>()
noteMap.put("downloadUrl", downloadUrl)
noteMap.put("userEmail", auth.currentUser!!.email.toString())
noteMap.put("noteTitle", titleText.text.toString())
noteMap.put("yourNote", noteText.text.toString())
noteMap.put("date", Timestamp.now())
db.collection("Notes").add(noteMap).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(
applicationContext,
exception.localizedMessage?.toString(),
Toast.LENGTH_LONG
).show()
}
}
}
}else {
val noteMap = hashMapOf<String, Any>()
noteMap.put("userEmail", auth.currentUser!!.email.toString())
noteMap.put("noteTitle", titleText.text.toString())
noteMap.put("yourNote", noteText.text.toString())
noteMap.put("date", Timestamp.now())
db.collection("Notes").add(noteMap).addOnCompleteListener { task ->
if (task.isComplete && task.isSuccessful) {
finish()
}
}
}
}
}
So all your logic is basically wrapped up in that putFile success callback, which requires an image to be successfully stored and retrieved (?) before anything will be added to the database.
You need to break that logic up, so you can only run the parts you want to - like just running the database update part if you don't have an image, or running that part later once your image is successfully handled.
So really, you need a "store in database" function to handle the final writing - call that directly if you don't have an image, call it in the success callbacks if you do. I'm just going to pseudocode this but:
saveData(noteMap: Map) {
add note to DB
}
saveClick() {
create noteMap with basic, non-image details (email, title etc)
if image is null, call saveData(noteMap)
else do the image stuff:
onSuccess:
add the downloadUrl to noteMap
call saveData(noteMap)
}
I see you've made an edit with an else branch which creates the map sans url and writes it - you're basically almost there, just make it a function instead, and pass the map in!

Unable to upload image clicked from camera to server

I'm trying to upload an image to the server after clicking from the camera but the server returns
($_File) JSON Response after clicking the image from server and uploading
{
"data":78,
"status":true,
"files":
{
"photo":
{
"name":"IMG_20191108_115642_5386652903586463966.jpg",
"type":"",
"tmp_name":"",
"error":1,
"size":0
}
}
}
($_File) JSON Response after picking the image from Gallery and uploading
{
"data":79,
"status":true,
"files":
{
"photo":
{
"name":"Screenshot_20191108_081937_com.instagram.android.jpg",
"type":"*\/*",
"tmp_name":"C:\\xampp\\tmp\\php50A6.tmp",
"error":0,
"size":518164
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pharmacy)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, permission, REQUEST_PERMISSION)
}
next.setOnClickListener {
if (prescriptionid == "") {
Toast.makeText(
applicationContext,
"Select/Upload Prescription First",
Toast.LENGTH_LONG
).show()
} else {
intent = Intent(applicationContext, SelectAddressActivity::class.java)
imageFilePath = ""
imageView.setImageResource(R.drawable.ic_image)
imagedisplay.visibility = View.GONE
startActivity(intent)
}
}
if (imageFilePath == "") {
imagedisplay.visibility = View.GONE
} else {
imagedisplay.visibility = View.GONE
}
}
Camera Intent
private fun openCameraIntent() {
val pictureIntent = Intent(
MediaStore.ACTION_IMAGE_CAPTURE)
if (pictureIntent.resolveActivity(getPackageManager()) != null)
{
try
{
photoFile = createImageFile()
}
catch (ex:IOException) {}// Error occurred while creating the File
if (photoFile != null)
{
val photoURI = FileProvider.getUriForFile(this, packageName+".provider", photoFile)
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI)
startActivityForResult(pictureIntent,CAMERA_REQUEST_CODE)
}
}
}
GalleryIntent
private fun pickFromGallery() {
//Create an Intent with action as ACTION_PICK
val intent = Intent(Intent.ACTION_PICK)
// Sets the type as image/*. This ensures only components of type image are selected
intent.type = "image/*"
//We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
val mimeTypes = arrayOf("image/jpeg", "image/png")
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
// Launching the Intent
startActivityForResult(intent, GALLERY_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
imagedisplay.visibility = View.VISIBLE
when (requestCode) {
CAMERA_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
correct.visibility = View.VISIBLE
imageView.setImageBitmap(setScaledBitmap())
}
}
GALLERY_REQUEST_CODE -> {
//data.getData returns the content URI for the selected Image
if (resultCode == Activity.RESULT_OK) {
correct.visibility = View.VISIBLE
var selectedImage = data!!.data as Uri
var filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
// Get the cursor
var cursor = getContentResolver().query(
selectedImage,
filePathColumn, null, null, null
);
// Move to first row
cursor!!.moveToFirst();
//Get the column index of MediaStore.Images.Media.DATA
var columnIndex = cursor.getColumnIndex(filePathColumn[0])
//Gets the String value in the column
var imgDecodableString = cursor.getString(columnIndex)
cursor.close()
// Set the Image in ImageView after decoding the String
Log.i("filepath", imgDecodableString)
imageFilePath = imgDecodableString
imageView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString))
}
}
else -> {
Toast.makeText(this, "Unrecognized request code", Toast.LENGTH_SHORT).show()
}
}
}
Code To Create image file
#Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES) as File
return File.createTempFile(
"IMG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
imageFilePath = absolutePath
}
}
Code To Upload Image To Server
val file = File(imageFilePath)
//creating request body for file
var requestBody = file.asRequestBody("*/*".toMediaTypeOrNull())
// val requestFile = file.asRequestBody("*/*".toMediaTypeOrNull())
var photo = MultipartBody.Part.createFormData("photo", file.name, requestBody)
// RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc);
Log.e("requestFile", imageFilePath)
val uploadImage =
RetrofitCall.provideRetrofit().create(uploadPrescriptionApi::class.java)
uploadImage.uploadPrescription("Bearer ".plus(sharedPreference!!.token.toString()), photo)
.enqueue(object : Callback<UploadPhototPOJO> {
override fun onResponse(
call: Call<UploadPhototPOJO>,
response: Response<UploadPhototPOJO>
) {
if (response.body()!!.status!!) {
progressDialog!!.dismiss()
prescriptionid = response.body()!!.id.toString()
Log.i("id", prescriptionid.toString())
} else {
Toast.makeText(
applicationContext,
"Oops Something Went Wrong!! Try again",
Toast.LENGTH_LONG
).show()
progressDialog!!.dismiss()
}
}
override fun onFailure(call: Call<UploadPhototPOJO>, t: Throwable) {
// handle execution failures like no internet connectivity
Log.i("Faliure", t.toString())
}
})
}
The maximum allowed size for uploading files is 2Mb. As you mentioned in your comment the size of the photo being uploaded is 4Mb.
If you want to upload files greater than the size set, just open the php.ini file to increase the size.
Open C:/xampp/php/php.ini file in any one of your favorite text editors. Search for upload_max_filesize and change it the size. For example, 50Mb
upload_max_filesize = 50M
And also the maximum size of POST data that PHP will accept is 8Mb. If you want to extend it, search for post_max_size and increase the size.
Finally, don't forget to restart the Apache server.

Using ACTION_IMAGE_CAPTURE get full image bitmap kotlin

All I want to do is allow users to take a photo and then after they've finished filling out a form submit that photo and upload to server.
I think I need the image in bitmap form so I can convert to Base64 string.
I've tried multiple different things I've found but none of them are working. I either get directory errors or intent null errors.
class Register : AppCompatActivity(), View.OnClickListener {
var PlayerID = ""
var mCurrentPhotoPath = ""
var stuff: File? = null
private var filePath: Uri? = null
private val PICK_IMAGE_REQUEST = 1
#RequiresApi(Build.VERSION_CODES.O)
override fun onClick(v: View?) {
if(v === buttonLoadPicture)
showFileChooser()
else if (v == btn_register)
uploadFile()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == PICK_IMAGE_REQUEST) {
val thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath)
PhotoImageView.setImageBitmap(thumbnail)
filePath = getRealPathFromURI(Uri.parse(filePath.toString()))
}
}
}
//////////////
fun getImageUri(inContext: Context , inImage: Bitmap): Uri {
val bytes = ByteArrayOutputStream()
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes)
val path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null)
return Uri.parse(path)
}
fun getRealPathFromURI(uri: Uri): Uri? {
val proj = arrayOf(MediaStore.Images.Media.DATA)
val cursor = getContentResolver().query(uri, proj, null, null, null)
cursor.moveToFirst()
val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
return Uri.parse(cursor.getString(idx))
}
////////////////
#RequiresApi(Build.VERSION_CODES.N)
private fun showFileChooser() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA),
1)
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
1)
}
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, 1)
}
val jsonobject = JSONObject()
#RequiresApi(Build.VERSION_CODES.O)
private fun uploadFile() {
val progress: ProgressBar = progressBarR
progress.visibility= View.VISIBLE
val iStream = contentResolver.openInputStream(filePath)
val bitmap = BitmapFactory.decodeStream(iStream)
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val m = baos.toByteArray()
val imageString = Base64.encodeToString(m,Base64.DEFAULT)
//get form data from register layout
jsonobject.put("FirstName", input_fname.text)
jsonobject.put("LastName", input_lname.text)
jsonobject.put("UserName", input_username.text)
jsonobject.put("Phone", input_phone.text)
jsonobject.put("DOB", input_DOB.text)
jsonobject.put("Photo", imageString)
jsonobject.put("Profile", input_profile.text)
jsonobject.put("Email", input_email.text)
jsonobject.put("Password", input_password.text)
jsonobject.put("ConfirmPassword", input_confirm.text)
var url = "https://elimination.azurewebsites.net/api/Account/Post"
val que = Volley.newRequestQueue(this#Register)
val req = JsonObjectRequest(Request.Method.POST, url, jsonobject,
Response.Listener<JSONObject>{
response -> response.toString()
//save PlayerID to val PlayerID
PlayerID = response.get("PlayerID").toString()
//save to sharedPreferences
val email = input_email.text.toString()
val mypref = getSharedPreferences(email, Context.MODE_PRIVATE)
val editor = mypref.edit()
editor.putString(email, PlayerID)
editor.apply()
val intent = Intent(this, Login::class.java)
startActivity(intent)
},
Response.ErrorListener{
response ->
Log.e("Something went wrong", response.toString())
})
//if server is slow
val socketTimeout = 30000 // 30 seconds
val policy = DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
req.setRetryPolicy(policy)
// Adding request to request queue
que.add(req)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
buttonLoadPicture.setOnClickListener(this)
btn_register.setOnClickListener(this)
}
}
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, 1)
You are invoking ACTION_IMAGE_CAPTURE without EXTRA_OUTPUT.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == PICK_IMAGE_REQUEST) {
val thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath)
PhotoImageView.setImageBitmap(thumbnail)
filePath = getRealPathFromURI(Uri.parse(filePath.toString()))
}
}
}
You are not using the Bitmap that you get back from ACTION_IMAGE_CAPTURE when you do not supply EXTRA_OUTPUT. Instead, you are looking in a filePath value that may well be null and certainly has nothing to do with the photo that the user's chosen camera app may have taken.
If you want to tell the camera app where to put a full-size photo, use EXTRA_OUTPUT. However, then, bear in mind that you are unlikely to be able to convert a full-size photo to base64, as you will run out of memory.

Categories

Resources