[英]share image from URL to other apps
Dont make this as duplicate..i have tried every link and i will show following what i have tried till now不要把它当作重复..我已经尝试了每个链接,我将按照我到目前为止所尝试的内容进行展示
i will briefly explain my code-->我将简要解释我的代码-->
fetching image from adapter to activity-->从适配器获取图像到活动-->
val bundle: Bundle = getIntent().getExtras()!!
val imgUrl: String = bundle.getString("image")!!
val imageUri = Uri.parse(imgUrl)
1-->>> 1-->>>
full code:--> referred from -->https://stackoverflow.com/questions/49011212/sharing-image-using-intent-on-whats-app-getting-error-sharing-failed完整代码:--> 引用自--> 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
}
Manifest-->清单-->
<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>
when sharing to whatsapp or any app the file format is not supported分享到 whatsapp 或任何应用程序时,不支持文件格式
2--->>>refered from this-->>> Share text OR Image On Whatsapp in Android 2--->>>引用自此-->>> 在 Android 中的 Whatsapp 上分享文本或图像
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"))
}
when sharing to whatsapp or any app the file format is not supported分享到 whatsapp 或任何应用程序时,不支持文件格式
3-->>> 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"))
when sharing to whatsapp or any app the file format is not supported分享到 whatsapp 或任何应用程序时,不支持文件格式
4-->>refered from this--> https://stackoverflow.com/a/25136183/12553303 4-->>引用自此--> 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..."))
when sharing to whatsapp or any app --> sharing failed,please try again later分享到 whatsapp 或任何应用时 --> 分享失败,请稍后再试
5--> but it is only sharing text not image 5-->但它只分享文本而不是图像
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, imgUrl)
sendIntent.type = "text/plain"
startActivity(sendIntent)
values in 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
i want to share an image need help thanks in advance我想分享一张图片需要帮助提前谢谢
You have to build content URI from the url.您必须从 url 构建内容 URI。 There are several ways to do this.
有几种方法可以做到这一点。
One way is to build that is download image from url and build URI from the downloaded file.一种方法是构建从 url 下载图像并从下载的文件构建 URI。
If you are using Glide to load image from url, then it can be done in following way:如果您使用Glide从 url 加载图像,则可以通过以下方式完成:
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..."))
}
})
Don't forget to add provider
in manifest:不要忘记在清单中添加
provider
:
<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>
and in provider_paths
并在
provider_paths
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache" path="/" />
</paths>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.