从onActivityResult获取共享价值

时间:2018-10-23 09:46:45

标签: android kotlin

你好,我有两个按钮在活动。首先,我拍摄一张照片,然后用onActivityResult覆盖,将照片上传到firebase中,然后获得一个URL,并将其保存到photoUrl中。然后在oncreate中的另一个btn侦听器中,我想使用此变量,但是在类的开头定义它时会得到null。如何获得我想要的photoUrl的值?

class AddYourStory : AppCompatActivity() {
    val    storage = FirebaseStorage.getInstance()

    private val REQUEST_IMAGE = 100
    private val TAG = "MainActivity"
    var destination: File? = null
    var imagePath: String? = null
    var photoUrl : String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_your_story)
        setSupportActionBar(toolbar)

        val db: FirebaseFirestore

        val intent = intent
        val lat = intent.getStringExtra("lng")
        val lng = intent.getStringExtra("lng")
        db = FirebaseFirestore.getInstance()
        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build());

        val name = dateToString(Date(), "yyyy-MM-dd-hh-mm-ss")
        destination = File(Environment.getExternalStorageDirectory(), "$name.jpg")

        val takephoto = findViewById<Button>(R.id.button2)
        takephoto.setOnClickListener {
            val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
            startActivityForResult(intent, REQUEST_IMAGE);
        }

        val txtTitle = findViewById<TextInputEditText>(R.id.textInputEditText2)
        val txtStory = findViewById<EditText>(R.id.editText)

        val btn = findViewById<Button>(R.id.button)
        btn.setOnClickListener{
            println("????????????????????????>>>>"+photoUrl)
            val MyStory: HashMap<String, String> = HashMap<String,String>()
            MyStory.put("title",txtTitle.text.toString())
            MyStory.put("story",txtStory.text.toString())
            MyStory.put("lat",lat)
            MyStory.put("lng",lng)
            MyStory.put("url",photoUrl.toString())
            db.collection("Stories").document().set(MyStory as Map<String, Any>)
            val confirm = Intent(this, MapsActivity::class.java)
            startActivity(confirm)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
            try {
                val `in` = FileInputStream(destination)
                val options = BitmapFactory.Options()
                options.inSampleSize = 10
                imagePath = destination!!.getAbsolutePath()
                val storageRef = storage.reference
                val stream = FileInputStream(File(imagePath))
                val picRef = storageRef.child(dateToString(Date(), "yyyy-MM-dd-hh-mm-ss"))

                val  uploadTask = picRef.putStream(stream)
                uploadTask.addOnFailureListener { exception ->
                    println("Failed")
                }.addOnSuccessListener { taskSnapshot ->
                    println("OK")
                    picRef.downloadUrl.addOnCompleteListener () {taskSnapshot ->
                        photoUrl = taskSnapshot.result.toString()
                        println ("url =" + photoUrl.toString ())
                    }
                }

                val bmp = BitmapFactory.decodeStream(`in`, null, options)
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            }
        } else {
            println("Cancel")
        }
    }

    fun dateToString(date: Date, format: String): String {
        val df = SimpleDateFormat(format)
        return df.format(date)
    }    
}

0 个答案:

没有答案