英文:
shared image file in Kotlin android studio
问题
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")
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile))
startActivity(Intent.createChooser(shareIntent, "Share image using"))
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)
英文:
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)
答案1
得分: 1
抛出的异常是:
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/app.xxxx.appimage/files/PicturesShared.jpg expobeyond app 通过 ClipData.Item.getUri() 共享
FileUriExposedException 是由以下原因引起的:
> 当应用向另一个应用公开 file:// Uri 时抛出的异常。
当目标 API 版本为 24 或更高版本时,您需要使用 内容提供程序 来与另一个应用共享文件。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论