I'm creating a file with this path
File file = new File(getExternalFilesDir(null) + "/package.apk");
then I'm trying to send it using this code
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(UpdateActivity.this, getPackageName() + ".provider", file), "application/vnd.android.package-archive");
Log.d("uri",FileProvider.getUriForFile(UpdateActivity.this, getPackageName() + ".provider", file).toString());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
my android manifest is
<provider
android:name="android.support.v4.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>
and the provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="files" path="." />
</paths>
After running code I'm getting There was a problem parsing the package error but if I open the file using file manager from path Android/my.package.name/files/package.apk it's parsing so the URI I'm sending is incorrect then I tried to print it and the result is
content://my.package.name.provider/files/package.apk
what am I doing wrong?
You forgot to add FLAG_GRANT_READ_URI_PERMISSION on the Intent. As it stands, the Uri is valid, but the installer has no rights to access its content.
Also note that the stock Android installer only supports content schemes as of Android 7.0. Older Android devices are limited to file.
Related
I have been working on CameraX API and storing captured image on Internal storage App data folder with path say storage/emulated/0/Android/data/packageName/Files/IMG_25052021_171806.jpg
Now i want to get content URI for this saved image. For this i have been using provider in Manifest.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.abc.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepath" />
</provider>
with File path as
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="/" />
When i am calling FileProvider.getUriForFile(ImageCrop.this,"com.abc.xyz.fileprovider", pictureFile); it returning me Illegal Argument Exception
Please suggest !!!
You seem to be using getExternalFilesDir() for the filesystem location. If so, use <external-files-path>, not <files-path>, in your metadata XML resource. See the documentation for more.
I am generating a CSV file from data pulled from Firebase and then trying to attach it to an email (and about two weeks ago this code worked!), but I keep getting the same error of a toast message saving the file cannot be attached and the following error in my log cat when I perform this task now:
2020-08-20 17:05:43.355 11147-11268/com.example.cpd E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.cpd.provider/external/MyCPDAudit.csv from pid=30180, uid=1000 requires the provider be exported, or grantUriPermission()
My code to generate the email Intent is as follows
//INTENT TO SEND USER EMAIL
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
//SETS THE USERS EMAIL AS THE RECIPIENT EMAIL ADDRESS
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{user.getEmail()});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Audit Profile");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Thank you for using CPD Journal to build your CPD Audit Profile. Please see the attached file for your records.");
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
File file = new File(csv);
Uri uri = FileProvider.getUriForFile(SavedAudit.this, "com.example.cpd.provider", file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
//USER CAN SELECT HOW THEY WANT TO SHARE THE CSV FILE
startActivity(Intent.createChooser(emailIntent, "Pick an email provider"));
I have the following permissions in my Android Manifest
<uses-permission android:name="android.permission.INTERNET" />
<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" />
File provider in Manifest is
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.cpd.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
My file paths are
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
As per the logcat, I have tried exporting the provider in my Manifest file, but this crashes the app on launch.
I have tried adding the following two lines with my email Intent, but this results in the same log cat error:
String packageName = getApplicationContext().getPackageName();
grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
My csv and uri log as follows
2020-08-20 18:03:30.969 20914-20914/com.example.cpd D/TAG: csv = /storage/emulated/0/MyCPDAudit.csv
2020-08-20 18:03:30.974 20914-20914/com.example.cpd D/TAG: uri = content://com.example.cpd.provider/external/MyCPDAudit.csv
However, I'm not sure if I'm using this grantUriPermission method correctly by calling it like this - any ideas how to solve this?
I'm having really hard time trying to open downloaded apk
I'm trying to use FileProvider (https://developer.android.com/reference/android/support/v4/content/FileProvider), I've followed this steps but I think I'm doing something wrong. I change this code in different ways many times, but unsuccessful.
Here's what I have:
File downloaded in:
app.setPathUpdate(Environment.getExternalStorageDirectory() +
File.separator + "trovata/update/" +
this.getApplicationInfo().packageName + File.separator)
The code above make this outcome: /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/
Here's what I'm doing:
SincronizacaoActivity.java
File FileAppInst = new File(app.getPathUpdate() + "atualizador");
Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", FileAppInst);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="atualizador" path="/trovata/update/${applicationId}/"/>
</paths>
Error:
...
Caused by: java.lang.IllegalArgumentException: Failed to find configured
root that contains /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/atualizador
...
Could you help me?
path="/trovata/update/${applicationId}/"
${applicationId} only turns into your application ID in AndroidManifest.xml. You cannot use manifest placeholders in resource files. So, replace ${applicationId} with your actual application ID.
In all the encounters with FileProvider it occurs that in most of the cases it only worked when the path was set to path=".".
Replace your filepaths.xml with this
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="atualizador" path="."/>
</paths>
And give it a try.
I have a strange problem when I try to share a my_file.html using ShareCompat.
As best practice I have created my own FileProvider but I get the error java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html I think I have set up all of the stuff correctly.
My XML I have created a provider for that.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.douglas.sample"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
This is my filepaths created in res/xml/filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path path="/forms" name="forms" />
</paths>
and also my method that is responsible to call share content.
public void shareContent() {
File file = new File("/forms/", fileName + ".html");
Uri uri = FileProvider.getUriForFile(getContext(), "com.douglas.sample", file);
Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
.setType(getActivity().getContentResolver().getType(uri))
.setStream(uri)
.getIntent();
//Provide read access
shareIntent.setData(uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_form)));
}
Finally my exception:
java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
at com.douglas.sample.TabbedFormsFragment$2.run(TabbedFormsFragment.java:254)
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:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Any Idea? thanks :D
It seems there are some little mistakes in your code.
Try the following:
<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>
and then in your provider_paths.xml try:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- We would like to give access to the External Storage of the app.
external_files will expose the path app to storage/emulated/0/ and the
"." is to actually point the root folder. The final path of the Uri will start
from path above and whatever folder we defined. For application upgrades
will be external_files/Android/data/package name/files/updates/name of update apk.-->
<external-path
name="external_files"
path="."/>
</paths>
and then whereever you want to use it try:
String path = Uri.parse(your_file_uri).getPath();
FileProvider.getUriForFile(application, PROVIDER, new File(path)),my_file_type);
where my_file_type = e.g. "application/vnd.android.package-archive" for .apk file
Hope it helps!!!
I have a problem with a project I am working on in Android Studio. The goal is to save a basic string into a .csv file then attach that file as an email attachment.
I cant write to the file, but I can share the file via FileProvider.getUriForFile.
I think the problem has something to do with these two linker error messages I get, the streamsavvy-1 is concerning bc my package name is com.team6.rifflegroup.streamsavvy
11-13 16:20:39.775 10286-10286/com.team6.rifflegroup.streamsavvy E/linker: readlink('') failed: No such file or directory [fd=31]
11-13 16:20:39.775 10286-10286/com.team6.rifflegroup.streamsavvy E/linker: warning: unable to get realpath for the library "/data/app/com.team6.rifflegroup.streamsavvy-1/oat/arm64/base.odex". Will use given name.
I build the file like this
File dataPath = new File(this.getFilesDir(), "xml");
File newData = new File(dataPath, FILE_NAME);
I have a paths.xml file within an xml directory under res that looks like this
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="my_files"
path="/"/>
</paths>
my write function looks like this
try {
FileWriter writer = new FileWriter(newData);
writer.append(csv);
writer.flush();
writer.close();
Toast.makeText(this, "SAVED", Toast.LENGTH_SHORT).show();
}catch (IOException e){
e.printStackTrace();
}
My Email intent looks like this
Uri contentUri = FileProvider.getUriForFile(this, "com.team6.rifflegroup.streamsavvy.FileProvider", newData);
this.grantUriPermission(getPackageName(), contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setType("vnd.android.cursor.dir/email");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"myEmail#gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, "File Attached");
emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
I give permission in my manifest here
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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=".dataLayout">
<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="com.team6.rifflegroup.streamsavvy.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/paths"/>
</provider>
</application>
I feel like this is a problem with the way java builds its projects but I am not very familiar with the process. Any help or pointers would be greatly appreciated.
This is my first time posting here so I am so sorry if my post is out of context or improperly formatted.
Apologize for the long post. Figured it out.
had to declare the .mkdirs() call (not .mkdir()) after specifying the file.