CreateSnapActivity
abstract class CreateSnapActivity : AppCompatActivity() {
var createSnapImageView :ImageView? = null
lateinit var filePath:Uri
var messageEditText : EditText? = null
val imagesNames = UUID.randomUUID().toString() + ".jpg"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_snap)
createSnapImageView = findViewById(R.id.createSnapImageView)
messageEditText = findViewById(R.id.messageEditText)
}
fun getPhoto(){
val intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
fun chooseImage(view: View){
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
1
)
}else{
getPhoto()
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
val selectedImage = data!!.data
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
try {
val bitmap =
MediaStore.Images.Media.getBitmap (this.contentResolver, selectedImage) createSnapImageView? .SetImageBitmap (bitmap) filePath = data.data !! } catch (e: Exception) {e.printStackTrace ()}}} переопределить удовольствие onRequestPermissionsResult (requestCode: Int, permissions: Array, grantResults: IntArray) {super.onRequestPermissionsResult (requestCode, permissions, grantResults) if (requestCode == 1) {if (grantResults.size> 0 && grantResults [0] == PackageManager.PERMISSION_GRANTED) {getPhoto ()}}} веселье дальше (просмотр: Просмотр) {
// Get the data from an ImageView as bytes
// Get the data from an ImageView as bytes
createSnapImageView?.setDrawingCacheEnabled(true)
createSnapImageView?.buildDrawingCache()
val bitmap = (createSnapImageView?.getDrawable() as BitmapDrawable).bitmap
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val data: ByteArray = baos.toByteArray()
var uploadTask: UploadTask = FirebaseStorage.getInstance().getReference().child("images").child(imagesNames).putBytes(data)
uploadTask.addOnFailureListener {
// Handle unsuccessful uploads
Toast.makeText(this,"Upload Failed :(",Toast.LENGTH_LONG).show()
}.addOnSuccessListener {
//taskSnapshot.getMetadata()
// ...
var ref = FirebaseStorage.getInstance().getReference().child("images").child(imagesNames)
uploadTask = ref.putFile(filePath)
var urlTask = uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
ref.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
Log.i("url", downloadUri.toString())
} else {
// Handle failures
// ...
}
}
}
}
}
Ошибка Logcat
2020-05-05 18:04:26.878 5847-18422/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=auto, ga_screen_class(_sc)=MainActivity, ga_screen_id(_si)=1579565357641318501}]
2020-05-05 18:04:26.896 5847-18422/? V/FA-SVC: Upload scheduled in approximately ms: 3599943
2020-05-05 18:04:26.896 5847-18422/? V/FA-SVC: Unscheduling upload
2020-05-05 18:04:26.902 5847-18422/? V/FA-SVC: Scheduling upload, millis: 3599943
2020-05-05 18:04:26.913 5847-18422/? V/FA-SVC: Background event processing time, ms: 35
2020-05-05 18:04:26.933 5847-18422/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=MainActivity, ga_previous_id(_pi)=1579565357641318501, ga_screen_class(_sc)=SnapsActivity, ga_screen_id(_si)=1579565357641318502}]
2020-05-05 18:04:26.952 5847-18422/? V/FA-SVC: Upload scheduled in approximately ms: 3599887
2020-05-05 18:04:26.952 5847-18422/? V/FA-SVC: Unscheduling upload
2020-05-05 18:04:26.958 5847-18422/? V/FA-SVC: Scheduling upload, millis: 3599887
2020-05-05 18:04:26.965 5847-18422/? V/FA-SVC: Background event processing time, ms: 33
2020-05-05 18:04:31.605 18327-18384/com.example.snapchat577 V/FA: Recording user engagement, ms: 7659
2020-05-05 18:04:31.645 18327-18384/com.example.snapchat577 V/FA: Activity paused, time: 26124529
2020-05-05 18:04:32.809 5847-18422/? V/FA-SVC: Logging event: origin=auto,name=user_engagement(_e),params=Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=7659, ga_screen_class(_sc)=SnapsActivity, ga_screen_id(_si)=1579565357641318502}]
2020-05-05 18:04:32.956 5847-18422/? V/FA-SVC: Upload scheduled in approximately ms: 3593883
2020-05-05 18:04:32.956 5847-18422/? V/FA-SVC: Unscheduling upload
2020-05-05 18:04:33.217 5847-18422/? V/FA-SVC: Scheduling upload, millis: 3593883
2020-05-05 18:04:33.401 5847-18422/? V/FA-SVC: Background event processing time, ms: 592
Приложение вылетает всякий раз, когда я пытаюсь запустить это действие .... ошибка не ясна, может ли кто-нибудь мне помочь ... приложение автоматически переходит в фоновый режим .... заранее спасибо ......