file:///storage/emulated/0/screenshot.png exposed beyond app through ClipData.Item.getUri()
You can go through this
You have add provider in android manifest and also create the a File Provider in xml/provider.xml as the answer in thus link suggests.
Fatal exception: exposed beyond app through ClipData, Camera crashes when clicked
The problem is from the ImageActivity.java. The code above gives error due to file Uri is exposed beyond clip data. The method above works only on SDKs <23 but for update this solution below solves it. I got the solution after reading so many documentation. Goodluck to everyone.
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent, CAMERA_REQUEST);
}
}
}
private File createImageFile() throws IOException{
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
fileDir = "file:" + image.getAbsolutePath();
return image;
}
});
myAlertDialog.show();
}
android.os.FileUriExposedException: exposed beyond app through Intent.getData() Exception , While sharing VCFile
Try below code it works for sharing VCFile:
private void shareContact(ContactsModal modal) {
String lookupKey = null;
Cursor cur = ((Activity)context).getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]
{ ContactsContract.Contacts.LOOKUP_KEY },
ContactsContract.Contacts._ID + " = " + modal.getContactID(), null, null);
if (cur.moveToFirst()) {
lookupKey = cur.getString(0);
}
if(lookupKey!=null){
Uri vcardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, vcardUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Contact Name");
((Activity)context).startActivity(intent);
}
}
android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
If your targetSdkVersion >= 24
, then we have to use FileProvider
class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting FileProvider
in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies as described here.
Steps to replace file://
URI with content://
URI:
- Add a FileProvider
<provider>
tag inAndroidManifest.xml
under<application>
tag. Specify a unique authority for theandroid:authorities
attribute to avoid conflicts, imported dependencies might specify${applicationId}.provider
and other commonly used authorities.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<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>
</manifest>
- Then create a
provider_paths.xml
file inres/xml
folder. A folder may be needed to be created if it doesn't exist yet. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder(path=".")
with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
The final step is to change the line of code below in
Uri photoURI = Uri.fromFile(createImageFile());
to
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());
Edit: If you're using an intent to make the system open your file, you may need to add the following line of code:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Please refer to the full code and solution that have been explained here.
Android android.os.FileUriExposedException Error in Android N
You must use FileProvider when you want to share files to other apps.
check https://developer.android.com/training/secure-file-sharing/setup-sharing.
use FileProvider.getUriForFile to get Uri instead of Uri.fromFile(file)
in you code.
FileProvider.getUriForFile(this,"your_package.fileprovider",photoFile);
Related Topics
How to Make an Array of Arrays in Java
Getresourceasstream() Is Always Returning Null
How to Get String Array from Arrays.Xml File
How to Download Older Google Play Services
Android Studio Convert Iso String to "America/New_York" When Adding to Event to Calendar
Java Lang Unsupportedclassversion Error in Xamarin Studio
Android Unable to Instantiate Activity: Didn't Find Class on Path
Fill Fields in Webview Automatically
Android: Realm + Retrofit 2 + Gson
Does Java Casting Introduce Overhead? Why
How to Limit the Number of Characters in Jtextfield
How to Check Available Space on Android Device? on Sd Card
Android Data Binding Using Include Tag
Stream Live Android Audio to Server
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Android: How to Parse Url String with Spaces to Uri Object
How to Get Elements of Fragments Created by Viewpager in Mainactivity