Here is my code can anybody suggest me what to do also NOTE: My app only supports from android 6 to 10 devices only so suggest me accordingly
private fun shareInvoice(){
binding.consLayout1.isDrawingCacheEnabled = true
binding.consLayout1.buildDrawingCache()
binding.consLayout1.drawingCacheQuality = View.DRAWING_CACHE_QUALITY_HIGH
val bitmap:Bitmap = binding.consLayout1.drawingCache
val sdf = SimpleDateFormat("dd-MM-yyyy hh:mm:ss", Locale.getDefault())
val root = Environment.getExternalStorageDirectory().absoluteFile
val file = File(root,"/Pictures")
val imageName = "ServiceInvoice_${businessName}_${sdf.format(System.currentTimeMillis())}"
val myFile = File(file,imageName)
if (myFile.exists()){
myFile.delete()
}
try {
val fos = FileOutputStream(myFile)
bitmap.compress(Bitmap.CompressFormat.JPEG,100, fos)
fos.flush()
fos.close()
showMessage("Invoice generated successfully")
binding.consLayout1.isDrawingCacheEnabled = false
Log.e("INVOICE TAG", "shareInvoice:$myFile")
}catch (e:Exception){
Log.e("FOS TAG", "shareInvoice:$e")
}
}
CodePudding user response:
Follow the method
private Uri saveImageExternal(Bitmap image) {
//TODO - Should be processed in another thread
Uri uri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "to-share.png");
FileOutputStream stream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.close();
uri = Uri.fromFile(file);
} catch (Execption e) {
Log.d(TAG, "IOException while trying to write file for sharing: " e.getMessage());
}
return uri;
}
private void shareImageUri(Uri uri){
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
startActivity(intent);
}
So Call
Uri uri = saveImageExternal(bitmap);
shareImageUri(uri);
Don't Forget to add in manifest and Request The Permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
CodePudding user response:
Here i get the final solution of my question if anybody wants to implement this functionality in their application Thank you people those who posted their suggestion and interest.
This code i have tested in Android version 9,10,11 and 7
private fun scanFile(path: String) {
try {
MediaScannerConnection.scanFile(context, arrayOf(path), null) { path, uri ->
Log.d(
"Tag",
"Scan finished. You can view the image in the gallery now."
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun shareInvoice(){
val file1 = File(
context?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.path,
Common.APP_DIRECTORY
)
if (!file1.exists()) {
file1.mkdirs()
}
val fileName = file1.absolutePath "/" System.currentTimeMillis() ".jpg"
val bitmap = binding.sectionScreenShot.getBitmapFromView()
try {
val fos = FileOutputStream(File(fileName))
bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.flush()
fos.close()
showMessage(getString(R.string.message_invoice_generated))
scanFile(fileName)
val uriForFile = FileProvider.getUriForFile(requireContext(),
requireContext().applicationContext.packageName ".provider",
File(fileName))
val share `enter code here`= Intent(Intent.ACTION_SEND)
share.type = "image/jpg"
share.putExtra(Intent.EXTRA_STREAM, uriForFile)
lifecycleScope.launch {
delay(1000)
startActivity(Intent.createChooser(share, "Share Invoice"))
}
} catch (e: Exception) {
Log.e("FOS TAG", "shareInvoice:$e")
}
}