attaching text file to Gmall app not working on Android 11 - android

I found out that I am unable to attach a file to a mail client that I'm opening from my app on Android 11.
It might be related to the storage access changes in Android 11, but I'm not sure how I need to adjust the code for it to be supported.
I store privately a log file with in external cache dir.
My intent is :
val intent = Intent(Intent.ACTION_SENDTO)
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("abc#gmail.com"))
intent.putExtra(Intent.EXTRA_STREAM, logFile)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = type
intent.data = Uri.parse("mailto:")
if (intent.resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(intent, "Send email via::"))
return true
}
and the file is :
val logFile = File(ctx.externalCacheDir, "MyLogFile.log")
Solution with FileProvider which didn't work :
I changed this line :
intent.putExtra(Intent.EXTRA_STREAM, logFile)
into this :
intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(activity, "${BuildConfig.APPLICATION_ID}.fileprovider", logFile))
and manifest :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
and provider_paths :
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path name="MyLogFile" path="."/>
</paths>

I solved with replacing:
val intent = Intent(Intent.ACTION_SENDTO)
with
Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("mime/type");
and in manifest:
<queries>
<intent>
<action android:name="android.intent.action.SEND"/>
<data android:mimeType="*/*" />
</intent>
</queries>

Related

Open internal storage image with Intent.ACTION_EDIT exception with FileProvider android

In my application there is a need where i have to EDIT an internal storage image and save the new to internal storage again. So i have tried to make an Intent.ACTION_EDIT in order to use the default Gallery app for editing the image.
if (it.fileName.fileType() == IMAGE_TYPE) {
createEditImageFile()
val intent = Intent(Intent.ACTION_EDIT).apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
putExtra(MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(this#MyActivity,BuildConfig.APPLICATION_ID,
File(currentEditPhotoPath)))
setDataAndType(uri, "image/*")
}
startActivity(intent)
return
}
So i get hte following exception which is reasonable since i dont have my FileProvider exported in the Manifest.
java.lang.SecurityException: Permission Denial: writing androidx.core.content.FileProvider uri content://{package_name}/external_path_files/Pictures/JPEG_20200720_163239_edit_5491322030710880687.jpg from pid=26681, uid=10206 requires the provider be exported, or grantUriPermission()
So when i change my in the Manifest.xml file to
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"
android:exported="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths" />
</provider>
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
and the paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="files"
path="." />
<external-files-path
name="external_path_files"
path="." />
<external-path
name="external_files"
path="."/>
</paths>
CreateEditImageFile() if the following
#Throws(IOException::class)
private fun createEditImageFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile("JPEG_${timeStamp}_edit_", ".jpg", storageDir).apply {
currentEditPhotoPath = absolutePath
}
}
i have the following fail on the Build.
Manifest merger failed : Attribute provider#androidx.core.content.FileProvider#exported value=(false) from AndroidManifest.xml:158:13-37
is also present at AndroidManifest.xml:167:13-36 value=(true).
Suggestion: add 'tools:replace="android:exported"' to <provider> element at AndroidManifest.xml:155:9-163:20 to override.
I dont really understand the error, and the most weird is that the AndroidManifest.xml lines are not where the provider is defined.
How can i solve this? Thank you

Getting unable to attach file error while attaching file in Gmail

I am trying to attach a pdf file from Downloads folder of internal storage to Gmail app using Intent. But it gives me error of Unable to attach the file. I am using FileProvider for getting the file of downloads folder. Here is the code that I have written.
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File pdfDirectory = new File(Environment.getExternalStorageDirectory(), "Download");
File pdfFile = new File(pdfDirectory, "fileName.pdf");
Uri pdfFileContentUri = FileProvider.getUriForFile(this, APPLICATION_ID + ".fileprovider", pdfFile);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_STREAM, pdfFileContentUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
AndroidManifests
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Can somebody guide how to solve this issue?

virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData

Please don't mark it duplicate before reading it
I have searched through all the links on stackoverflow and Github for a possible answer but still am getting the same error.
Here is the Manifest file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="com.example.aditya.pdf_report"
android:exported="false" android:grantUriPermissions="true"
android:name="android.support.v4.content.FileProvider"
android:readPermission="com.company.aditya.pdf_report.READ">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
</application>
Here is Java file:
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf_report/" + reportname.getText().toString().trim() + ".pdf" );
Uri path;
path = FileProvider.getUriForFile(MainActivity.this, getPackageName(),filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Here is the Branch Visit Report - Human Resources");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
here is file-paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="pdf_report" path="pdf_report/"/>
</paths>
here is my package name:
com.example.aditya.pdf_report
Here is where the Pdf is saved on my phone:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf_report"
I have followed all the links but I think its the file-paths.xml where I am writing wrong stuff
change your file-path.xml as below. It may be work.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>

Install application in Android N programmatically

I want to install an apk in my application and I'm using this method but all the time I get the errors listed below:
Code
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.apk");
Uri fileUri = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(DLActivity.this, "codenevisha.com.apps.bartarinapp.provider",file);
}
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
And this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="codenevisha.com.apps.bartarinapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".utils.G"
android:allowBackup="true"
android:icon="#mipmap/logo_chabok"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="codenevisha.com.apps.bartarinapp.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/paths"/>
</provider>
<activity
android:name=".activity.ActivitySplash"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".activity.DLActivity">
</activity>
</application>
</manifest>
I used this path.xml file:
<external-files-path
name="share"
path="."/>
But when I run my application get this error:
Process: codenevisha.com.apps.bartarinapp, PID: 21392
java.lang.RuntimeException: Unable to start activity ComponentInfo{codenevisha.com.apps.bartarinapp/codenevisha.com.apps.bartarinapp.activity.DLActivity}: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/test.apk
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/test.apk
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at codenevisha.com.apps.bartarinapp.activity.DLActivity.onCreate(DLActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
Why is this error happening?
Your FileProvider is misconfigured. Your FileProvider metadata needs an <external-path> element, and you do not have one. See the documentation.
I tested the below code.
Add below permission in Menifest
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Below code for installing package
//// TODO: 1/16/18 Check the external storage permission
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.apk");
Uri fileUri = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {
String packageId = getApplicationContext().getPackageName();
fileUri = FileProvider.getUriForFile(MainActivity.this, packageId + ".files", file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
Provider in Menifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.files"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
file_paths.xml
<paths>
<external-path name="external" path="/"/>
Per the Android documentation on EXTRA_NOT_UNKNOWN_SOURCE:
For this to work you must start the installer with startActivityForResult().

Send file from FileProvider to another app

I'm trying to send file to another app for using Intent. I used targetSdkVersion 25 in my gradle. My app works fine on android version < 24. When android version >= 24, i'm using FileProvider for sending file to another app and it's not working here.
This is step I using for FileProvider:
manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="sp.xxx.yyy.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"/>
</provider>
file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/sp.xxx.yyy/" name="app_root" />
<external-path path="Android/data/sp.xxx.yyy/files/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
Open intent:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
File file = new File(path);
Log.e("Utils" , "file exists: " + file.exists());
Uri contentUri = FileProvider.getUriForFile(context, "sp.xxx.yyy.fileProvider", file);
intent.setData(contentUri);
} else {
Uri contentUri = Uri.parse(path);
intent.setData(contentUri);
}
context.startActivity(intent);
When use FileProvider for sending file uri to another app, I see that other app receive data like this:
Intent { act=android.intent.action.VIEW dat=content://sp.xxx.yyy.fileProvider/external_storage_root/Android/data/sp.xxx.yyy/files/filetosending.txt flg=0x10400002 cmp=sp.zzz/sp.zzz.MainActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
Another app not works with data received content://sp.xxx.yyy.fileProvider/external_storage_root/Android/data/sp.xxx.yyy/files/filetosending.txt
What's wrong in my code? Have any suggestion for resolve this problem? Thank your support.

Categories

Resources