Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将图像从URL共享到其他应用程序_Android_Image_Kotlin_Android Intent_Share - Fatal编程技术网

Android 将图像从URL共享到其他应用程序

Android 将图像从URL共享到其他应用程序,android,image,kotlin,android-intent,share,Android,Image,Kotlin,Android Intent,Share,不要让这重复..我已经尝试了每一个链接,我会显示以下我已经尝试到现在 我将简要解释我的代码-> 正在将图像从适配器提取到活动-> val bundle: Bundle = getIntent().getExtras()!! val imgUrl: String = bundle.getString("image")!! val imageUri = Uri.parse(imgUrl) 1->> 完整代码:->引用自->https://stac

不要让这重复..我已经尝试了每一个链接,我会显示以下我已经尝试到现在

我将简要解释我的代码->


正在将图像从适配器提取到活动->

     val bundle: Bundle = getIntent().getExtras()!!

     val imgUrl: String = bundle.getString("image")!!
     val imageUri = Uri.parse(imgUrl)
1->>

完整代码:->引用自->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
}
舱单->

 <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>
以上代码输出:-> 共享到whatsapp或任何应用程序时,文件格式不受支持

3->>

以上代码输出:-> 共享到whatsapp或任何应用程序时,文件格式不受支持

4->>参考此->

以上代码输出:-> 当共享到whatsapp或任何应用->共享失败时,请稍后重试

5->但它只共享文本,不共享图像

                   val sendIntent = Intent()
                    sendIntent.action = Intent.ACTION_SEND
                    sendIntent.putExtra(Intent.EXTRA_TEXT, imgUrl)
                    sendIntent.type = "text/plain"
                    startActivity(sendIntent)
日志中的值:-

   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

我想分享一张图片,需要帮助,请提前感谢

您必须从url构建内容URI。有几种方法可以做到这一点

一种方法是从url下载图像并从下载的文件生成URI

如果您使用从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..."))

            }
        })
和在提供程序路径中


正在将图像从适配器提取到活动-您可能正在尝试在活动之间传递Uri。这将不能很好地工作,这取决于您从何处获取Uri。除此之外,使用一个具体的MIME类型,而不是通配符。我在清单中使用了…这还不够@commonware,还可以看到我的第三个方法,其中我加载的是改装响应well,您的Uri需要是一个内容Uri。改造不会给你带来这些。此外,你的清单与此无关。在您的意图中,使用一个具体的MIME类型,而不是通配符。@commonware如果我这样使用->sendIntent.putExtraIntent.EXTRA_STREAM,imgUrl sendIntent.type=image/jpeg如果我选择whatsapp,它返回到显示共享失败的应用程序,请稍后重试imgUrl的值是什么?获取崩溃->由以下原因引起:android.util.AndroidRuntimeException:从活动上下文外部调用startActivity需要标志\u Activity\u NEW\u TASK标志。这真的是你想要的吗?尝试将FLAG\u ACTIVITY\u NEW\u TASK FLAG添加到intent。它在加载ACTIVITY时起作用,但我想在单击共享按钮时共享图像。查看我的代码。我还通过代码->Glide.withgetApplicationContext.loadres?.body!!在我的活动中加载了该图像!!。data.product_images.get0.image.into imagemain;我已经在回答的第一行描述了它。问题是您的URI没有任何内容。这就是为什么这些都不起作用。
                   val sendIntent = Intent()
                    sendIntent.action = Intent.ACTION_SEND
                    sendIntent.putExtra(Intent.EXTRA_TEXT, imgUrl)
                    sendIntent.type = "text/plain"
                    startActivity(sendIntent)
   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
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..."))

            }
        })
    <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>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache" path="/" />
</paths>