I am making A meme creator application. I am not able to share the bitmap of a layout(rl_meme_container) as image to other platforms like whatsapp. I tried to see various comments and posts by different users but none worked for me. This is the code i tried to share bitmap as image:
val file_path:String = Environment.getExternalStorageDirectory().absolutePath + "/ProjectName"
val dir:File =File(file_path);
if(!dir.exists())
dir.mkdirs();
val file:File = File(dir,"share")
try {
val fOut:FileOutputStream =FileOutputStream(file)
rl_meme_container.drawToBitmap().compress(Bitmap.CompressFormat.PNG, 85, fOut)
fOut.flush();
fOut.close();
} catch (e:Exception) {
e.printStackTrace();
}
var uri:Uri = Uri.fromFile(file);
val intent:Intent = Intent();
intent.action = Intent.ACTION_SEND;
intent.type = "image/*";
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "")
intent.putExtra(android.content.Intent.EXTRA_TEXT, "")
intent.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(intent, "Share Cover Image"))
Its showing this error:
FATAL EXCEPTION: main
Process: com.meme.finalmeme, PID: 26583
android.os.FileUriExposedException: file:///storage/emulated/0/ProjectName/share exposed beyond app through ClipData.Item.getUri()