Getting unable to attach file error while attaching file in Gmail - android

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?

Related

Error while analyzing package during apk installation in app

I want to have the abilities to update my app from my application, so I can download my apk file ( each of them are signed ). And when the installation want to start:
First of all: I have preparation of installation popup which appear
Then: another popup appear and told me "A problem occurred while parsing the package"
My error is like this:
So my app, didn't update. Could you help me please ?
I use Xamarin Forms but especially for Android.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.MyCompany.clamexdroid" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application android:label="ClamexDroid" android:theme="#style/MainTheme" android:icon="#mipmap/ic_launcher">
</application>
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
My function when I click on my update Button
private async Task update()
{
this.popupIsVisible = false;
this.IsBusy = true;
string uri = "myLink.apk";
HttpClient cli = new HttpClient();
await cli.DownloadBasicFile(uri, DependencyService.Get<IDeviceDependency>().GetExternalAPKPath());
DependencyService.Get<IDeviceDependency>().InstallApk();
this.IsBusy = false;
}
And my InstallApk() function ( I'm sure that the file is downloaded )
public void InstallApk()
{
Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(
Android.Net.Uri.Parse($"content:///{this.GetExternalAPKPath()}"),"application/vnd.android.package-archive");
promptInstall.SetFlags(ActivityFlags.NewTask);
promptInstall.AddFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(promptInstall);
}
public string GetExternalAPKPath()
{
try
{
return Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.MyCompany.clamexdroid.apk");
}
catch (Exception e) { Console.WriteLine(e.Message); }
return "";
}
At first, you can check the apk file you had downloaded has any damage. And then you can try to the following steps to install the apk:
1.Add the provider in the AndroidManifest.xml, such as:
<application android:allowBackup="true" android:icon="#drawable/androidicon" android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme">
<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>
</application>
2.Create the provider_paths.xml in the folder named xml below the values folder in the android part.
<paths >
<external-path
name="external_files"
path="." />
</paths>
3.Use the following code to install the apk:
var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.MyCompany.clamexdroid.apk");
Java.IO.File file = new Java.IO.File(path);
Intent install = new Intent(Intent.ActionView);
Android.Net.Uri apkURI = FileProvider.GetUriForFile(this, this.ApplicationContext.PackageName + ".provider", file);
install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
install.AddFlags(ActivityFlags.NewTask);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
StartActivity(install);

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().

Java.Lang.IllegalArgumentException using FileProvider

I'm getting this exception when trying to use FileProvider:
Java.Lang.IllegalArgumentException: Unable to find configured root for content://com.abc/Android/data/com.abc/files/345345345.pdf Java.Lang.IllegalArgumentException
I guess my file_paths.xml configures the FileProvider with the wrong path. My code:
file_paths.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="." name="." />
<files-path path="com.abc/Android/data/com.abc/files/" name="files" />
</paths>
androidManifest.xml
<application android:label="#string/AppName" android:icon="#drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.abc" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths" />
</provider>
</application>
What values should I put in path and name in file_paths.xml, to expose the file with the content URI: content://com.abc/Android/data/com.abc/files/345345345.pdf?
my code which creates the URI:
File newFile = new File (Application.Context.FilesDir.AbsolutePath, filename);
Android.Net.Uri androidContentUri = FileProvider.GetUriForFile (Application.Context, Application.Context.PackageName, newFile);
System.Uri systemContentUri = SystemUri (androidContentUri);
return systemContentUri;
Your entry:
<files-path path="com.abc/Android/data/com.abc/files/" name="files" />
Should become:
<files-path path="files" name="files" />
Path in this case represents the files in the files/ subdirectory of your app's internal storage area.
This application private subdirectory is the same as the value returned:
Application.Context.FilesDir;
So the file location within your app is:
var path = Path.Combine(Application.Context.FilesDir.AbsolutePath, "files");

NullPointerException when trying to get URI with FileProvider(Android) [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
i want to take a Photo after clicking on a button. But i am getting NullPointerException when trying to get URI with FileProvider.
Here is my ErrorCode:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at ibas.orosol.BildActivity.takePicture(BildActivity.java:86)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  
The Problem-Line seems to be:
mUri = FileProvider.getUriForFile(getApplication().getApplicationContext(),
"ibas.provider", file);
Here´s my Code:
public void takePicture(View view) throws IOException {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try{
File file = createImageFile();
mUri = FileProvider.getUriForFile(getApplication().getApplicationContext(),
"ibas.provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
} catch (IOException e){
e.printStackTrace();
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/orosol/captured_image.jpg";
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Log.i("uri-data", "Uri: " + mUri.toString());
mBitmap = getBitmapFromUri(mImageView,BildActivity.this,mUri);
mImageView.setImageBitmap(mBitmap);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
My Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ibas.orosol">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="ibas.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
<activity
android:name=".HeizoelActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MenuActivity" />
</activity>
<activity
android:name=".BildActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MenuActivity" />
>
</activity>
<activity
android:name=".FacebookActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MenuActivity" />
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".KontakteActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MenuActivity" />
</activity>
<activity android:name=".UeberUnsActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MenuActivity" />
</activity>
</application>
Provider in res/xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/> </paths>
Any Idea where the Problem Occurs?
The provider tag is outside of the application tag in the manifest file .
You should add it inside the <application></application> tag
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="ibas.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>

Categories

Resources