Попробуйте использовать этот код, например, для ссылки:
private fun getFileExtension(uri: Uri?): String? {
val cR = activity!!.contentResolver
val mime = MimeTypeMap.getSingleton()
return mime.getExtensionFromMimeType(cR.getType(uri!!))
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
val selectedfile = data!!.data //The uri with the location of the file
val fname = getFileName(selectedfile)
val file = FileUtils.getFile(activity, selectedfile)
val storage = FirebaseStorage.getInstance()
val storageRef = storage.reference
val progressDialog = ProgressDialog(getContext())
progressDialog.setTitle("Uploading")
progressDialog.show()
val fileReference = mStorageRef!!.child(System.currentTimeMillis().toString()
+ "." + getFileExtension(selectedfile))
var bitmap: Bitmap? = null
try {
bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, selectedfile)
} catch (e: IOException) {
e.printStackTrace()
}
val baos = ByteArrayOutputStream()
bitmap!!.compress(Bitmap.CompressFormat.JPEG, 20, baos)
val dat = baos.toByteArray()
val uploadTask = fileReference.putBytes(dat)
uploadTask.addOnFailureListener { exception -> Log.i("whatTheFuck:", exception.toString()) }.addOnSuccessListener {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
progressDialog.dismiss()
Toasty.success(getContext()!!, "Image Uploaded", Toast.LENGTH_LONG, true).show()
uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
}
fileReference.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
//DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getInstance().getCurrentUser().getUid());
val url = downloadUri!!.toString()
Log.i("seeThisUri", downloadUri.toString())// This is the one you should store
db!!.collection(s!!).document(docname!!)
.update(
"imageurl", url
)
// Upload upload = new Upload(editTextName.getText().toString().trim(),
// Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
// String uploadId = ref.push().getKey();
//ref.child(uploadId).setValue(upload);
} else {
Toasty.info(getContext()!!, "Image Not Selected", Toast.LENGTH_LONG, true).show()
}
}
}.addOnProgressListener { taskSnapshot ->
val progress = 100.0 * taskSnapshot.bytesTransferred / taskSnapshot.totalByteCount
progressDialog.setMessage("Uploaded " + progress.toInt() + "%...")
}
} else if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK) {
val extras = data!!.extras
val selectedfile = extras!!.get("data") as Bitmap
val imageuri = getImageUri(activity!!, selectedfile)
val fname = getFileName(imageuri)
val file = FileUtils.getFile(activity, imageuri)
val storage = FirebaseStorage.getInstance()
val storageRef = storage.reference
val progressDialog = ProgressDialog(getContext())
progressDialog.setTitle("Uploading")
progressDialog.show()
val fileReference = mStorageRef!!.child(System.currentTimeMillis().toString()
+ "." + getFileExtension(imageuri))
var bitmap: Bitmap? = null
try {
bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, imageuri)
} catch (e: IOException) {
e.printStackTrace()
}
val baos = ByteArrayOutputStream()
bitmap!!.compress(Bitmap.CompressFormat.JPEG, 20, baos)
val dat = baos.toByteArray()
val uploadTask = fileReference.putBytes(dat)
uploadTask.addOnFailureListener { exception -> Log.i("whatTheFuck:", exception.toString()) }.addOnSuccessListener {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
progressDialog.dismiss()
Toasty.success(getContext()!!, "Image Uploaded", Toast.LENGTH_LONG, true).show()
uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
}
fileReference.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
//DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getInstance().getCurrentUser().getUid());
val url = downloadUri!!.toString()
Log.i("seeThisUri", downloadUri.toString())// This is the one you should store
db!!.collection(s!!).document(docname!!)
.update(
"imageurl", url
)
// Upload upload = new Upload(editTextName.getText().toString().trim(),
// Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
// String uploadId = ref.push().getKey();
//ref.child(uploadId).setValue(upload);
} else {
Toasty.info(getContext()!!, "Image Not Selected", Toast.LENGTH_LONG, true).show()
}
}
}.addOnProgressListener { taskSnapshot ->
val progress = 100.0 * taskSnapshot.bytesTransferred / taskSnapshot.totalByteCount
progressDialog.setMessage("Uploaded " + progress.toInt() + "%...")
}
}
}
private fun getImageUri(context: Context, inImage: Bitmap): Uri {
val bytes = ByteArrayOutputStream()
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
val path = MediaStore.Images.Media.insertImage(context.contentResolver, inImage, "Title", null)
return Uri.parse(path)
}
fun getFileName(uri: Uri?): String {
var result: String? = null
if (uri!!.scheme == "content") {
val cursor = activity!!.contentResolver.query(uri, null, null, null, null)
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
} finally {
cursor!!.close()
}
}
if (result == null) {
result = uri.path
val cut = result!!.lastIndexOf('/')
if (cut != -1) {
result = result.substring(cut + 1)
}
}
return result
}