PDF file when opening in Android Nougat is showing blank screen - android

I am creating a PDF file and saving it in local storage. When trying to open it, it is working perfect in all devices except in Android N. I am able to open PDF file in Android N using FileProvider, but it is displaying as blank.
This is my URI
content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
This is my code
Uri path;
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ "Android" + "/" + "data" + "/" + "com.products" + "/" + file);
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
} else {
path = Uri.fromFile(pdfFile);
}
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
}

The problem is with how you are setting the flags on the intent
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Instead, try this:
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Grzegorz Matyszczak's solution worked for my case. I had a very similar setup to Sabya Sachi. More specifically this line allowed me to see the PDF file (URI from FileProvider.getURIForFile call):
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
More info on grantUriPermissions here, under android:grantUriPermissions section.

As I can see that you have used FileProvider for Nougat.
You have to add a FileProvider tag in AndroidManifest.xml.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
</provider>
A FileProvider can only generate a content URI for files in directories that you specify beforehand.To link this file to the FileProvider, add a element as a child of the element that defines the FileProvider.
<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/file_paths"/>
</provider>
You must specify a child element of for each directory that contains files for which you want content URIs. You can add them to a new file called res/xml/file_paths.xml. For example ,these XML elements specify two directories:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>
->you have to set your PDF directory path in file_paths.xml
For more detail refer this

Replace FileProvider class with MyFileProvider class:
path = MyFileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
where MyFileProvider should be a generic class which extends FileProvider:
public class MyFileProvider extends FileProvider {
}
Modify AndroidManifest by setting your MyFileProvider class location to android:name attribute in your provider section:
android:name=".utils.MyFileProvider"

Related

File from FileProvider can't be viewed

I'm struggling with FileProvider. I want to open a video in another app, but no matter what I try every single app says that the video can't be loaded.
private void passVideo2(String videoname) {
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File videoInternalPath = new File(this.getFilesDir(), videoname);
File newFile = new File(videoPath, videoname);
Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
viewIntent.setDataAndType(uri, getContentResolver().getType(uri));
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
this.startActivity(viewIntent);
}
I've tried getting the video from the external and from the internal storage and I've tried different video formats and other file types like e.g. pdf and nothing seems to work
This is my provider_paths.xml
<paths>
<files-path name="files" path="/"/>
<external-path name="external" path="video_folder"/>
</paths>
and this is from the manifest
<provider
android:authorities="com.example.provider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>

File passed with FileProvider can't be viewed

I'm struggling with FileProvider. I want to open a video in another app, but no matter what I try every single app says that the video can't be loaded.
private void passVideo2(String videoname) {
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File videoInternalPath = new File(this.getFilesDir(), videoname);
File newFile = new File(videoPath, videoname);
Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
viewIntent.setDataAndType(uri, getContentResolver().getType(uri));
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
this.startActivity(viewIntent);
}
I've tried getting the video from the external and from the internal storage and I've tried different video formats and other file types like e.g. pdf and nothing seems to work
This is my provider_paths.xml
<paths>
<files-path name="files" path="/"/>
<external-path name="external" path="video_folder"/>
</paths>
and this is from the manifest
<provider
android:authorities="com.example.provider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
This is a repost from this question as it didn't get any answers

Opening PDF file using Android intent

So here's the Scenario:
I am downloading a PDF from network and saving it in /0/Download/NSIT - Notices/"fileName".pdf
Now sending Intent like this:
private void openPDF(String fileName) {
Log.d(TAG, "openPDF: called");
Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(Environment.getExternalStorageDirectory() + "/Download/NSIT - Notices", fileName + ".pdf");
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", myPDF);
Log.d(TAG, "openPDF: intent with uri: " + uri);
intent.setDataAndType(uri, "application/pdf");
context.startActivity(Intent.createChooser(intent, "Open with..."));
}
Now any PDF reader (Google Drive PDF Reader, System PDF Viewer) is saying
The File Does Not Exist
Image:
The Google one is sitting not doing anything (that's why not included its image)
Mainfest.xml
<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>
Provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
I am creating File here in AsyncTask but opening from the postExecute of that AsyncTask
File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(myDir, params[1] + ".pdf");
if (!myDir.exists())
myDir.mkdirs();
try {
fileOutputStream = new FileOutputStream(myPDF);
fileOutputStream.write(response.bodyAsBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "doInBackground: Download complete");
Logcat
Previously it was throwing FileUriExposedException then I fixed with this FileProvider thing. The problem is the pdf file downloaded by my app is created successfully and I am able to open it successfully from any File Manager (ES File Explorer now) but not from my app :(. I am new to FileProvider. Need help!
PS: That's not my device problem either. Tested with two other phones which has different ROMs and API >= 23
Please set intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
It will grant access of your file to other providers
See documentation for File provider
Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application
can use the provider's content URI to access it, subject to the
permissions specified for the provider.
false: The provider is not available to other applications.
access to the provider to your applications. Only applications that
have the same user ID (UID) as the provider will have access to it.
Try to change android:exported="true"

Unable to launch a PDF viewer in Nougat (7.1)

I am creating a app in which i am generating pdf and after this app showing PDF automatically. it is working wee in 6.1 android but not showing in android 7.1. my code is
public void showPDF() {
File pdfFile = new File(Environment.getExternalStorageDirectory() , "A_DailyRegiser.pdf");
try {
if (pdfFile.exists()) {
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
// objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
objIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
objIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(objIntent);
} else {
Toast.makeText(dailycollection.this, "File NotFound",Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(dailycollection.this,
"No Viewer Application Found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
That's because of the new changes in Noughat API's Read more here
you are suggested to use the content:// scheme instead of file:// for this we can use the file provider API.
here is sample code:
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
...
</application>
define the scope of your access
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/"/>
Generating the Content URI for a File
File imagePath = new File(Context.getFilesDir(), "images");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
Granting Temporary Permissions to a URI
Intent.setFlags() with either FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION or both.
read more here

Failed to find configured root that contains

I'm sorry to ask an overly repeated question, but any of the answer here on stackoverflow really helped me.
I want to use a FileProvider to access file to a custom folder. My declaration in the manifest, inside the <application> tag:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
then, in provider_paths.xml
<paths>
<cache-path name="my_videos" path="MyFolderName/"/>
</paths>
(but I tried, instead of cache-path, files-path, external-path and external-files-path)
The creation of my files is:
public static Uri getUriFromFile(File file, Context context) {
int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
context.grantUriPermission(context.getPackageName(), uri, flags);
return uri;
}
public static File getMyDirectory() {
File root = new File(context.getCacheDir(), "MyFolderName");
if(root.mkdir()) Log.e(Consts.TAG, "Directory created");
return root;
}
...
File outputFile = new File(getMyDirectory(), filename + ".mp4");
videoUri = getUriFromFile(outputFile, this);
(and, instead of context.getCacheDir(), I tried context.getFilesDir(), Environment.getExternalStorageDirectory() and context.getExternalFilesDir(""))
and finally, I want to retrieve my file:
String pathToFile = "/my_videos/filename.mp4";
Uri file = getUriFromFile(new File(pathToFile), context);
tried with paths:
"/MyFolderName/filename.mp4"
"content://my.package.name.provider/my_videos/filename.mp4"
"content://my.package.name.provider/MyFolderName/filename.mp4"
ALL of this tries brought me to this error:
Failed to find configured root that contains /my_videos/filename.mp4
Any idea on what I am doing wrong? Thanks in advance!
I want to retrieve my file:
None of those are valid paths to the file.
You already have code that gives you a valid path to the file:
File outputFile = new File(getMyDirectory(), filename + ".mp4");
So, use the code that you already wrote.

Categories

Resources