Two file providers in android app image fileprovider not working - android

I am sharing the app with the file provider and I want to share the image from the bitmap.
I have two file providers number one is for share apk, number two for share image. apk file provider works good but the image share file provider not sharing images. if I share an image to WhatsApp, Gmail it says file format does not support it.
fileprovider.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path
name="apk"
path="/"/>
</paths>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path name="img" path="my_images/"/>
</paths>
imagesharefrombitmap.kt
imgBtnShare.setOnClickListener {
shareImage()
loadingDialoglordshiva.startDialog()
}
}
private fun shareImage() {
val bitmapShare = getBitmapFromView(relLayout)
val filename = "${System.currentTimeMillis()}.jpg"
val cachePath = File(externalCacheDir.toString() + "/my_images")
cachePath.mkdirs()
//create a png file
//create png file
val file = File(cachePath, filename)
val fileOutputStream: FileOutputStream
try {
fileOutputStream = FileOutputStream(file)
bitmapShare.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
fileOutputStream.flush()
fileOutputStream.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
val myImageFileUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider_paths",file)
//create a intent
//create a intent
val intent = Intent(Intent.ACTION_SEND)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_STREAM, myImageFileUri)
intent.type = "image/jpg"
startActivity(Intent.createChooser(intent, "Share with"))
loadingDialoglordshiva.dismissDialog()
}
Manifest_file_only_provider_showed
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.lordshiva.myapplication.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/fileprovider"
/>
</provider>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.lordshiva.myapplication.provider_paths"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
if i run this share intent is opening. but share to WhatsApp,Gmail or something its says file format is not supported . help me I don't know how to solve this.

I am solved by share bitmap image directly to intent.

Related

How to show image in android remote view using content URI?

I am having trouble trying to show an image in a remote view with a contentUri. I have outlined the steps I took below. I believe I have followed the procedure correctly but still get a "Can't load widget" message. What am I doing wrong? Is the image saved to the correct storage area? Is the contentUri constructed correctly? Please help.
Blockquote
Save the image into my file directory. I can see the image successfully saved in data/data/com.mydomain/files/myImage.jpg when I check using Android Studio's Device File Explorer.
val file = File(applicationContext.filesDir, "myImage.jpg")
try {
FileOutputStream(file).use { out ->
myBitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)
}
} catch (e: IOException) {
e.printStackTrace()
}
Create a FileProvider in manifest and provider_paths.xml in res.xml
<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>
<paths>
<files-path name="name" path="." />
</paths>
Create a content Uri using getUriForFile. This returns a content uri of: content://com.mydomain.provider/name/myImage.jpg
val file = File(getAppInstance().filesDir, "myImage.jpg")
val contentUri = FileProvider.getUriForFile(
getAppInstance(),
BuildConfig.APPLICATION_ID + ".provider",
file
)
I pass the content Uri into Image() composable.
Image(
modifier = GlanceModifier.size(28.dp),
provider = ImageProvider(contentUri),
contentDescription = "Image"
)
Result on run:

Unable to add attachment to email (Android, Kotlin)

I have an export button in my app that executes the below code. The email app opens correctly with the subject etc., but it's missing the attachment. I don't seem to be able to get the attachment to work. Any ideas what I'm doing wrong?
val filename = "Export.csv"
val path = context?.getExternalFilesDir(null) //get file directory for this package
//create fileOut object
val fileOut = File(path, filename)
//delete any file object with path and filename that already exists
fileOut.delete()
//create a new file
fileOut.createNewFile()
//append the header and a newline
fileOut.appendText("Case Number,Form Type,Status,Last Updated Date")
fileOut.appendText("\n")
for (item in arrayAll) {
fileOut.appendText("${item.number},${item.formType},${item.status},${item.LUD}")
fileOut.appendText("\n")
}
val contentUri = FileProvider.getUriForFile(this.requireContext(),"${BuildConfig.APPLICATION_ID}.fileProvider", fileOut)
val emailIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:") // only email apps should handle this
putExtra(Intent.EXTRA_SUBJECT, "Export.csv")
putExtra(Intent.EXTRA_STREAM, contentUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
this.startActivity(emailIntent)
Manifest:
<provider
android:authorities="${applicationId}.fileProvider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path name="root" path="." />
</paths>
Following from you latest comment; According to the Android docs Intent.ACTION_SENDTO is for when you don't want attachments, I think you need to change this line val emailIntent = Intent(Intent.ACTION_SENDTO).apply { to
val emailIntent = Intent(Intent.ACTION_SEND).apply { to get your email app to display the attachment

android 10 camera image issue

i am getting trying to get image from camera in android 10 but it is not working properly.i have used this code for getting image from camera
this is my intent
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, createImageFile())
startActivityForResult(takePhotoIntent, 3)
her it is my file provider
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="images"
path="." />
</paths>
here it is my function to create file path
public static File createImageFile(
Context context,
String dirName,
String fileName,
String fileType) {
try {
File file = createDir(context, dirName);
File image = new File(file.getAbsoluteFile() + File.separator + fileName + fileType);
if (!image.getParentFile().exists()) {
image.getParentFile().mkdirs();
}
image.createNewFile();
return image;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
here it is file provider define in manifest file
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.android.restuarantuser.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>

Don't attach file and send by email

I define FileProvider in manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="root"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths"/>
</provider>
and write paths :
<files-path path="files/" name="files_name" />
I put file "1.txt" in "root"/files/1.txt
Created intent for send file by email:
val intentToSendToBd = Intent(Intent.ACTION_SEND)
val file = File(context.filesDir,"files/1.txt")
val ur = FileProvider.getUriForFile(context, "root", file )
intentToSendToBd.setType("text/plain")
intentToSendToBd.putExtra(Intent.EXTRA_STREAM, ur)
intentToSendToBd.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intentToSendToBd.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
And finally I get "couldn't attach file" in EmailApp.
I am using this code to share mp3 file using send_imtent
val sdCard = Environment.getExternalStorageDirectory()
val directory = File(sdCard.absolutePath + "/Demo Mobile/VoiceMail")
val f = File(directory, sharingFileName + ".mp3") //or any other format supported
**val uri = Uri.parse("file://" + f.absolutePath)**
val share = Intent(Intent.ACTION_SEND)
share.putExtra(Intent.EXTRA_STREAM, uri)
share.setType("audio/mp3")
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(share, "Share Voicemail File"))
Thanks, Happy Coding...

Share Image of image view with intent in Kotlin

I have an option in my app that share image to whatsapp,facebook etc. For share image through intent, i want specific image from image view on which share button is clicked.
I have the following code that does not work.it share an empty file to whatsapp.
val shareBtn = findViewById<TextView>(R.id.share_btn)
val postImage = findViewById<ImageView>(R.id.post_image)
val path:String?=postImage.tag.toString()
val file= File(path)
shareBtn.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image"
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(intent, "Share Image"))
First to store image of ImageView , you need to convert to Bitmap
val bitMap : Bitmap =imageview.getDrawingCache();
now store this image to file
val bos : ByteArrayOutputStream = ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
val file : File = File(Environment.getExternalStorageDirectory() + File.separator + "your_file.jpg");
try {
file.createNewFile();
val fos : FileOutputStream = FileOutputStream(file);
fos.write(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
now create an intent by specifying type 'image/jpeg'
and setting extra stream and path of the file that is to be shared
val intent= new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/your_file.jpg"));
And start Activity by creating chooser
startActivity(Intent.createChooser(intent, "Share Image"));
a great way for share image file ( Kotlin ) :
first create a folder named xml in the res folder and create a new XML Resource File named provider_paths.xml and put the below code inside it :
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="files"
path="."/>
<external-path
name="external_files"
path="."/>
</paths>
now go to the manifests folder and open the AndroidManifest.xml and then put the below code inside the <application> tag :
<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_paths.xml file path in this example
</provider>
now you put the below code in the setOnLongClickListener :
button.setOnLongClickListener {
try {
val file = File("pathOfFile")
if(file.exists()) {
val uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file)
val intent = Intent(Intent.ACTION_SEND)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setType("image/*")
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent)
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
toast("Error")
}
}

Categories

Resources