如何使用活动结果将图像从片段上传到firebase? (科特林)

时间:2018-06-12 11:03:23

标签: android firebase android-fragments kotlin firebase-storage

尝试从片段上传图片到firebase。我也使用这个命令启动活动:activity !! .startActivityForResult ...... 但仍然是同样的错误。

我以前用于裁剪图像和压缩图像的库:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
implementation 'id.zelory:compressor:2.1.0'

changeProfile是一个按钮

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

继承我的活动结果,我首先检查活动结果中的错误。我将croped图像作为个人资料图像上传到firebase存储。

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

0 个答案:

没有答案