Skip to content
Advertisement

shared image file in Kotlin android studio

I have an application in which I try to share a photo, but when you share the application it crashes.

    val bitmap = (imageViewx1.drawable as BitmapDrawable).bitmap
    val nombreArchivo:String="Shared.jpg"
    val patchload= getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + nombreArchivo
    val mifile2 =File(patchload)
    val stream: OutputStream = FileOutputStream(mifile2)
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
    stream.flush()
    stream.close()
    val shareIntent = Intent(Intent.ACTION_SEND)
    shareIntent.type = "image/jpg"
    val photoFile = File(filesDir, "PicturesShared.jpg")
    Toast.makeText(this, photoFile.toString(), Toast.LENGTH_SHORT).show()
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile))
    startActivity(Intent.createChooser(shareIntent, "Share image using"))

log:

Process: app.xxxx.appimage, PID: 21588
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/app.xxxx.appimage/files/PicturesShared.jpg expobeyond app through ClipData.Item.getUri()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1967)
    at android.net.Uri.checkFileUriExposed(Uri.java:2364)
    at android.content.ClipData.prepareToLeaveProcess(ClipData.java:941)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:9747)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:9753)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:9732)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1679)
    at android.app.Activity.startActivityForResult(Activity.java:4477)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
    at android.app.Activity.startActivityForResult(Activity.java:4435)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
    at android.app.Activity.startActivity(Activity.java:4800)
    at android.app.Activity.startActivity(Activity.java:4768)
    at app.xxxx.appimage.flip$onCreate$4.onClick(flip.kt:97)
    at android.view.View.performClick(View.java:6259)
    at android.view.View$PerformClick.run(View.java:24732)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6694)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

Advertisement

Answer

The exception raised is

android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/app.xxxx.appimage/files/PicturesShared.jpg expobeyond app through ClipData.Item.getUri()

FileUriExposedException is caused by

The exception that is thrown when an application exposes a file:// Uri to another app.

When targeting Api 24 or higher you need to use a content provider to share a file with another app.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement