URL에서 다른 앱으로 이미지 공유

Aug 19 2020

이것을 중복으로 만들지 마십시오. 나는 모든 링크를 시도했으며 지금까지 시도한 것을 다음과 같이 보여줄 것입니다.

내 코드를 간단히 설명하겠습니다 .-->

어댑터에서 활동으로 이미지를 가져 오는 중->

     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 또는 모든 앱에 공유 할 때 파일 형식이 지원되지 않습니다.

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"))
               }

위 코드 출력 :->

whatsapp 또는 모든 앱에 공유 할 때 파일 형식이 지원되지 않습니다.

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"))

위 코드 출력 :->

whatsapp 또는 모든 앱에 공유 할 때 파일 형식이 지원되지 않습니다.

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..."))

위 코드 출력 :->

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

이미지를 공유하고 싶어요 미리 감사드립니다

답변

1 shafayathossain Aug 21 2020 at 08:48

URL에서 콘텐츠 URI를 작성해야합니다. 이를 수행하는 방법에는 여러 가지가 있습니다.

한 가지 방법은 URL에서 이미지를 다운로드하고 다운로드 한 파일에서 URI를 빌드하는 것입니다.

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..."))

            }
        })

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>

그리고 provider_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache" path="/" />
</paths>