Reputation: 13
I was able to successfully download the PDF file from the server(Web API) using the following code
HttpURLConnection connection = null;
File file;
File outputFile = null;
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
int count;
string filename = "xxx.pdf"
try {
//output path would be "/data/user/0/package.ofapplication/files/folder/"
file = new File(interface.getFileDirectory(),"folder");
if (!file.isDirectory()) file.mkdirs();
//output would be "/data/user/0/package.ofapplication/files/folder/xxx.pdf"
outputFile = new File(file, filename);
URL url = new URL(getUrlMethod());
connection = (HttpURLConnection) url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
inputStream = new BufferedInputStream(url.openStream());
fileOutputStream = new FileOutputStream(outputFile);
.......other
.......related
.......downloading codes
//this would return "/data/user/0/package.ofapplication/files/folder/xxx.pdf"
return outputFile.getPath();
}
Now I'm getting an error when I'm trying to view the PDF file using the following code in Android.
// output path would be /data/user/0/package.ofapplication/files/folder/
File pdfFile = new File(pdfPath);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW)
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//this is the part where im getting an error
pdfIntent.setDataAndType(FileProvider.getUriForFile(this.getActivity(), this.getActivity().getApplicationContext().getPackageName() + ".provider",pdfFile), "application/pdf");
The error message I'm getting from the above code is
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/package.ofapplication/files/folder/1.pdf
Can somebody please help me on this I'm crying and searching for hours right now and still can't find a way. I also tried this link but I'm still having the same error
FileProvider - IllegalArgumentException: Failed to find configured root
Note: my provider_path below
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Upvotes: 1
Views: 52
Reputation: 24848
It seems like you are trying to store the file on the internal storage
and defines path element on XML as the external storage
like <external-path
, So try to change path element on XML as the internal storage
:
<external-path
TO
<files-path
More info. https://developer.android.com/reference/android/support/v4/content/FileProvider
Upvotes: 1