compartir la imagen de la URL a otras aplicaciones
No hagas esto como duplicado ... he probado todos los enlaces y mostraré lo que he probado hasta ahora
explicaré brevemente mi código ->
obteniendo la imagen del adaptador a la actividad ->
val bundle: Bundle = getIntent().getExtras()!!
val imgUrl: String = bundle.getString("image")!!
val imageUri = Uri.parse(imgUrl)
1 - >>>
código completo: -> referido desde -> https://stackoverflow.com/questions/49011212/sharing-image-using-intent-on-whats-app-getting-error-sharing-failed
val bundle: Bundle = getIntent().getExtras()!!
val imgUrl: String = bundle.getString("image")!!
val imageUri = Uri.parse(imgUrl)
shareiamge.setOnClickListener {
shareImage(imageUri)
}
private fun shareImage(imagePath: Uri) {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
sharingIntent.type = "image/*"
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath)
//sharingIntent.setPackage("com.whatsapp"); for whatsapp only
startActivity(
Intent.createChooser(
sharingIntent,
"Share Image Using"
)
) // for all generic options
}
Manifiesto ->
<activity
android:name=".ProductDetails.Product_details"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.SEND" /> <!-- Send
action required to display activity in share list -->
<category android:name="android.intent.category.DEFAULT" /> <!--
Make activity default to launch -->
<!-- Mime type i.e. what can be shared with this activity only image and text -->
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
salida de código anterior: ->
al compartir en whatsapp o cualquier aplicación, el formato de archivo no es compatible
2 --- >>> referido desde esto - >>> Compartir texto O imagen en Whatsapp en Android
shareiamge.setOnClickListener {
val whatsappIntent = Intent(Intent.ACTION_SEND)
whatsappIntent.type = "image/*"
whatsappIntent.putExtra(
Intent.EXTRA_STREAM,
imageUri
) //add image path
startActivity(Intent.createChooser(whatsappIntent, "Share image using"))
}
salida de código anterior: ->
al compartir en whatsapp o cualquier aplicación, el formato de archivo no es compatible
3 - >>>
val bundle: Bundle = getIntent().getExtras()!!
val imgUrl: String = bundle.getString("image")!!
val imageUri = Uri.parse(imgUrl)
shareiamge.setOnClickListener {
val whatsappIntent = Intent(Intent.ACTION_SEND)
whatsappIntent.type = "image/*"
whatsappIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.parse(res?.body()!!.data.product_images.get(0).image)
) //direct image from retrofit response
startActivity(Intent.createChooser(whatsappIntent, "Share image using"))
salida de código anterior: ->
al compartir en whatsapp o cualquier aplicación, el formato de archivo no es compatible
4 - >> referido desde esto -> https://stackoverflow.com/a/25136183/12553303
val bundle: Bundle = getIntent().getExtras()!!
val imgUrl: String = bundle.getString("image")!!
val imageUri = Uri.parse(imgUrl)
shareiamge.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image")
val path: String =
MediaStore.Images.Media.getContentUri(imgUrl).toString()
val screenshotUri = Uri.parse(path)
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri)
intent.type = "image/*"
startActivity(Intent.createChooser(intent, "Share image via..."))
salida de código anterior: ->
al compartir en whatsapp o cualquier aplicación -> error al compartir, intente nuevamente más tarde
5 -> pero solo comparte texto, no imagen
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, imgUrl)
sendIntent.type = "text/plain"
startActivity(sendIntent)
valores en log: -
Log.e("imgUrl",imgUrl)
Log.e("imageUri", imageUri.toString())
E/imgUrl: http://....../uploads/prod_img/thumb/medium/9dc6234da018916e545011fa1.jpeg
E/imageUri: http://..../uploads/prod_img/thumb/medium/9dc6234da018916e545011fa1.jpeg
quiero compartir una imagen necesito ayuda gracias de antemano
Respuestas
Tienes que crear contenido URI a partir de la URL. Hay varias formas de hacer esto.
Una forma es compilar esa imagen de descarga desde la URL y compilar URI desde el archivo descargado.
Si está utilizando Glide para cargar la imagen desde la url, puede hacerlo de la siguiente manera:
Glide.with(context).asBitmap().load(photoUrl)
.into(object: CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
// do your stuff, you can load placeholder image here
}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val cachePath = File(context.cacheDir, "images")
cachePath.mkdirs() // don't forget to make the directory
val stream = FileOutputStream(cachePath.toString() + "/image.png") // overwrites this image every time
resource.compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.close()
val imagePath = File(context.cacheDir, "images")
val newFile = File(imagePath, "image.png")
val contentUri: Uri = FileProvider.getUriForFile(context, "${BuildConfig.APPLICATION_ID}.provider", newFile)
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_STREAM, contentUri)
context.startActivity(Intent.createChooser(intent, "Choose..."))
}
})
No olvide agregar el provider
manifiesto:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
y en provider_paths
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache" path="/" />
</paths>