Kotlin, onActivityResult is deprecated scanning QR code, any ideas - kotlin

"onActivityResult" is deprecated
I don't know how to replace with "StartActivityForResult" any ideas
please help, This is my code:
my code works fine but showme warning about deprecated method
this is my function QrScan code:
private fun scanQRCode() {
val integrator = IntentIntegrator(this).apply {
captureActivity = CaptureActivity::class.java
setOrientationLocked(false)
setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
setPrompt("Scanning Code")
}
integrator.initiateScan()
}
then
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) Toast.makeText(this, "OperaciĆ³n Cancelada", Toast.LENGTH_LONG).show()
else {
resultado = result.contents.toString()
getlist()
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
I tried this new way but I can't get it works
the new way in Kotlin:
var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
doSomeOperations()
}
}
fun openSomeActivityForResult() {
val intent = Intent(this, SomeActivity::class.java)
resultLauncher.launch(intent)
}

The initiateScan() method calls another method called createScanIntent() which returns the intent necessary for the resultLauncher to work. This way:
private fun scanQRCode() {
val integrator = IntentIntegrator(this).apply {
captureActivity = CaptureActivity::class.java
setOrientationLocked(false)
setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
setPrompt("Scanning Code")
}
resultLauncher.launch(integrator.createScanIntent())
}
Replacing integrator.initiateScan() with resultLauncher.launch (integrator.createScanIntent()) accesses the result of resultLauncher

In your new way of kotlin
replace StartActivityForResult() with ActivityResultContracts.StartActivityForResult() and instead of
resultLauncher.launch(intent) you can use
resultLauncher.launch(
IntentIntegrator(requireActivity())
.setOrientationLocked(true)
.setPrompt("scanning")
.createScanIntent()
)

Related

Error While Upload Image From Galley Using Drjacky Image Picker And Retrofit 2

hi guys I got an error like the one below when trying to upload an image from the galley:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{efed650 15275:app.fadlyproject.com/u0a158} (pid=15275, uid=10158) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
the error comes from this line of code:
val parcelFileDescriptor =
contentResolver.openFileDescriptor(selectedImageUri!!, "r", null) ?: return
I'm using 3rd party libraries namely DrJacky and Retrofit 2. I've added some necessary things to the manifest as below:
Dependencies :
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.github.Drjacky:ImagePicker:2.3.19'
Manifest :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
image_view = findViewById(R.id.image_view)
button_upload = findViewById(R.id.button_upload)
image_view!!.setOnClickListener {
openImageChooser()
}
button_upload!!.setOnClickListener {
uploadImage()
}
}
private val profileLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val uri = it.data?.data!!
selectedImageUri = uri
image_view!!.setImageURI(selectedImageUri)
} else parseError(it)
}
private fun openImageChooser() {
ImagePicker.with(this)
.provider(ImageProvider.BOTH)
.setDismissListener {
Log.d("ImagePicker", "onDismiss");
}
.createIntentFromDialog { profileLauncher.launch(it) }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_CODE_PICK_IMAGE -> {
selectedImageUri = data?.data
image_view!!.setImageURI(selectedImageUri)
}
}
}
}
private fun uploadImage() {
if (selectedImageUri == null) {
layout_root!!.snackbar("Select an Image First")
return
}
val parcelFileDescriptor =
contentResolver.openFileDescriptor(selectedImageUri!!, "r", null) ?: return
val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor)
val file = File(cacheDir, contentResolver.getFileName(selectedImageUri!!))
val outputStream = FileOutputStream(file)
inputStream.copyTo(outputStream)
progress_bar!!.progress = 0
val body = UploadRequestBody(file, "image", this)
MyAPI().uploadImage(
MultipartBody.Part.createFormData(
"file",
file.name,
body
),
RequestBody.create(MediaType.parse("multipart/form-data"), "json")
).enqueue(object : Callback<UploadResponse> {
override fun onFailure(call: Call<UploadResponse>, t: Throwable) {
layout_root!!.snackbar(t.message!!)
progress_bar!!.progress = 0
}
override fun onResponse(
call: Call<UploadResponse>,
response: Response<UploadResponse>
) {
response.body()?.let {
layout_root!!.snackbar(it.message)
progress_bar!!.progress = 100
classes!!.text = it.data.classes
layout!!.visibility = View.VISIBLE
Glide.with(this#MainActivity).load("http://167.172.72.26:1337/"+ it.data.image_after_preprocessing).into(
image_view!!
)
}
}
})
}
override fun onProgressUpdate(percentage: Int) {
progress_bar!!.progress = percentage
}
companion object {
const val REQUEST_CODE_PICK_IMAGE = 101
}
This answer is late but it might help others.
I just added .crop() to it like this.
ImagePicker.with(this)
.crop()
.provider(ImageProvider.BOTH)
.setDismissListener {
Log.d("ImagePicker", "onDismiss");
}
.createIntentFromDialog { profileLauncher.launch(it) }
I noticed that the error only persist when using the gallery and adding the .crop() is the only solution to it.
In case you don't use Crop option, you just need to add:
mGalleryUri?.let { galleryUri ->
contentResolver.takePersistableUriPermission(
galleryUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
https://developer.android.com/training/data-storage/shared/photopicker#persist-media-file-access
I'll change ACTION_GET_CONTENT to ACTION_OPEN_DOCUMENT on the next update.
[And if I could find a way to have both worlds(having crop and let developer use or not use takePersistableUriPermission, I'll update again.]

How to call handlePurchase() here? suspend function inside non-suspend

just wondering if its possible to call suspend fun handlePurchase() inside:
suspend fun handlePurchase(purchase: Purchase) {
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged) {
val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
val ackPurchaseResult = withContext(Dispatchers.IO) {
val client = BillingClient.newBuilder(this#GoproActivity).build()
client.acknowledgePurchase(acknowledgePurchaseParams.build())
}
}
}
}
private val purchasesUpdatedListener = PurchasesUpdatedListener { billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
handlePurchase(purchase) --->> here is the problem
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
}
}
inside onCreate:
var billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases().build()
val skuList = ArrayList<String>()
skuList.add("dons.dogs.04")
button.setOnClickListener {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
// TODO("Not yet implemented")
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
billingClient.querySkuDetailsAsync(params.build()) { billingResult, skuDetailsList ->
for (skuDetails in skuDetailsList!!) {
val flowPurchase = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build()
val responseCode = billingClient.launchBillingFlow(
this#GoproActivity,
flowPurchase
).responseCode
}
}
}
}
override fun onBillingServiceDisconnected() {
TODO("Not yet implemented")
}
})
}
Everything should be Ok after that. Until now purchases are happening but canceled/refunded because are not acknowledged. I tried to follow different tutorials and resources also the official documentation but dont understund how to implement this part here. As i said the "purchasing" part works properly but can not acknowledge them. Is there any easier way to do that?
Thanks in advance.
You can use,
lifeCycleScope.launch{
//your suspend function here
}

I want to upload image on server using Volley Library

I'm selecting the image from my phone gallery and want to upload image on server but my app crash every time i don't know the reason.. i already study many tutorials and Question but i did not understand. please help me.
Here is my code
class profileCreate : AppCompatActivity() {
var context: Context? = null
var imageUri: Uri? = null
var picturePath: String? = null
val url = "https://apps.faizeqamar.website/charity/api/donnor_add"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile_create)
val et_name = findViewById<EditText>(R.id.et_name)
val et_cnic = findViewById<EditText>(R.id.et_cnic)
val et_email = findViewById<EditText>(R.id.et_email)
val et_phon = findViewById<EditText>(R.id.et_phon)
val et_address = findViewById<EditText>(R.id.et_address)
val profile_image = findViewById<ImageView>(R.id.profile_image)
profile_image.setOnClickListener {
checkPermission()
}
val btn_create_profile = findViewById<Button>(R.id.btn_create_profile)
btn_create_profile.setOnClickListener {
imageUpload()
}
}
Volley Code
private fun imageUpload() {
val smr =
SimpleMultiPartRequest(
Request.Method.POST, url,
Response.Listener { response ->
Log.d("Response", response)
Toast.makeText(
applicationContext,
"xyz",
Toast.LENGTH_LONG
).show()
}, Response.ErrorListener { error ->
Toast.makeText(
applicationContext,
error.message,
Toast.LENGTH_LONG
).show()
})
var fname = et_name.text.toString()
var cnic = et_cnic.text.toString()
var email = et_email.text.toString()
var phone = et_phon.text.toString()
var address = et_address.text.toString()
smr.addFile("user_image", picturePath)
smr.addStringParam("fname", fname)
smr.addStringParam("cnic", cnic)
smr.addStringParam("email", email)
smr.addStringParam("phone", phone)
smr.addStringParam("address", address)
val mRequestQueue = Volley.newRequestQueue(applicationContext)
mRequestQueue.add(smr)
}
Pick Image from phone
//*********pick image from phone************
var READIMAGE: Int = 253
fun checkPermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(
this,
android.Manifest.permission.READ_EXTERNAL_STORAGE
) !=
PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
READIMAGE
)
return
}
}
loadImage()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
READIMAGE -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadImage()
} else {
Toast.makeText(
applicationContext,
"cannot access your images",
Toast.LENGTH_LONG
).show()
}
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
val PICK_IMAGE_CODE = 123
fun loadImage() {
var intent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
startActivityForResult(intent, PICK_IMAGE_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_CODE && data != null && resultCode == RESULT_OK) {
imageUri = data.data
val filePathColum = arrayOf(MediaStore.Images.Media.DATA)
val cursor = contentResolver.query(imageUri!!, filePathColum, null, null, null)
cursor!!.moveToFirst()
val columnIndex = cursor.getColumnIndex(filePathColum[0])
picturePath = cursor.getString(columnIndex)
cursor.close()
profile_image?.setImageBitmap(BitmapFactory.decodeFile(picturePath))
}
}
}
Encode your image to string and send to the server like sending strings
change your onACtivityresult as below
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
try {
if (requestCode == PICK_IMAGE_CODE && data != null && resultCode == RESULT_OK) {
val contentURI = data!!.data
try {
logBitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
encodedImgString = getStringImage(logBitmap!!)
profile_image!!.setImageBitmap(logBitmap)
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show()
}
}else if (requestCode== IMAGE_CAPTURE_CODE){
img_logo.setImageURI(image_uri)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
here is the getStringImage function
private fun getStringImage(bmp: Bitmap): String {
val baos = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val imageBytes = baos.toByteArray()
return Base64.encodeToString(imageBytes, Base64.DEFAULT)
}
You can send the encodedImgString to the server and decode from there

Flutter - Get data with an Event Channel from Kotlin to Dart

I have the following problem that I am already working on for over 20 hours: I want to use an Event Channel to get a data stream from the Spotify SDK. On the native side, I can automatically display the status of a current song by subscribing to my PlayerState. My goal is to be able to access this data stream with my Flutter app. On the native side I can output the data flow without problems. But I also want to be able to access this data in my Flutter App. The problem is that I do not get the data from Kotlin to Dart. I can not execute the command mEventSink?.success(position) because the mEventSink is zero.
It would be really great if someone could help me with this problem.
//...
class Spotifysdk04Plugin(private var registrar: Registrar): MethodCallHandler, EventChannel.StreamHandler {
//...
private var mEventSink: EventChannel.EventSink? = null
companion object {
#JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "spotifysdk")
channel.setMethodCallHandler(Spotifysdk04Plugin(registrar))
val eventChannel = EventChannel(registrar.messenger(), "timerStream")
eventChannel.setStreamHandler(Spotifysdk04Plugin(registrar))
}
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "loginAppRemote") {
//...
} else if(call.method == "initEventStream") {
try {
spotifyAppRemote!!.playerApi.subscribeToPlayerState()
.setEventCallback { playerState: PlayerState? ->
Log.d("test", "test24")
var position = playerState!!.playbackPosition.toDouble()
Log.d("playbackPosition1", position.toString())
if(mEventSink != null) {
Log.d("test", "test25")
mEventSink?.success(position)
} else {
Log.d("test", "mEventSink == null")
}
}
} catch (err:Throwable) {
Log.v("initEventStreamError",err.message.toString())
result.success(false)
}
} else {
result.notImplemented()
}
}
override fun onCancel(arguments: Any?) {
mEventSink = null
}
override fun onListen(arguments: Any?, eventSink: EventChannel.EventSink) {
mEventSink = eventSink
}
}
I found a solution:
override fun onListen(p0: Any?, p1: EventChannel.EventSink?) {
mEventSink = p1
Log.d("test", "test1")
if(spotifyAppRemote == null) {
Log.d("test", "test2")
}
val connectionParams = ConnectionParams.Builder(clientId)
.setRedirectUri(redirectUri)
.showAuthView(true)
.build()
SpotifyAppRemote.connect(registrar.context(), connectionParams, object : Connector.ConnectionListener {
override fun onConnected(appRemote: SpotifyAppRemote) {
spotifyAppRemote = appRemote
if(spotifyAppRemote != null) {
Log.d("test", "test3")
spotifyAppRemote!!.playerApi.subscribeToPlayerState()
.setEventCallback { playerState: PlayerState? ->
Log.d("test", "test24")
var position = playerState!!.playbackPosition.toDouble()
Log.d("playbackPosition1", position.toString())
if(mEventSink != null) {
Log.d("test", "test25")
mEventSink?.success(position)
} else {
Log.d("test", "mEventSink == null")
}
}
}
Log.d("Spotify App Remote Login", "Connected!")
}
override fun onFailure(throwable: Throwable) {
Log.e("Spotify App Remote Login", "Error!", throwable)
}
})
}

how do i get downloadurl from below code?

Below is the code I checked and it did not work.
I did get not url but get "com.google.android.gms.tasks.zzu#ffbfff5"
I think
val url=taskSnapshot.getMetadata()?.getReference()?.getDownloadUrl()?.toString()
should be amended but I dont know how to fix.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == PICK_PROFILE_FROM_ALBUM && resultCode == Activity.RESULT_OK) {
var imageUri = data?.data
val uid = FirebaseAuth.getInstance().currentUser!!.uid
FirebaseStorage
.getInstance()
.reference
.child("userProfileImages")
.child(uid)
.putFile(imageUri!!)
.addOnSuccessListener { taskSnapshot ->
val url = taskSnapshot.getMetadata()?.getReference()?.getDownloadUrl()?.toString()
val map = HashMap<String, Any>()
map["image"] = url
FirebaseFirestore.getInstance().collection("profileImages").document(uid).set(map)
}
}
}
}