How can I change caching time? I receive a notification when the PDF file is updated on the server. I get the same link, but with another (previous) document. Since URL hashes the previous document, it opens the old one, not the updated one.
Android.Net.Uri uri = Android.Net.Uri.Parse(url);
Intent browserIntent = new Intent(Intent.ActionView, uri);
try {
context.StartActivity(browserIntent);
}
catch (ActivityNotFoundException e) {
Console.WriteLine(e);
Toast.MakeText(context, "Install PDF reader", ToastLength.Short);
}
My URL doesn't change, however, pdf file changes.
I solved my problem, I just changed the URL. And now every URL to the pdf file is unique and not cached.
url = url.Replace(" ", "%20");
url = $"{url}?random_number={new Date().Time}";
Related
hello i want to show pdf file in webview or in external app.
1) i use this url
https://docs.google.com/gview?embedded=true&url= +pdfurl , but google says "Whoops There was a problen while previewing document"
2) if i use intent to open the url in external app then they didnot load the file in external app.
3) if i use the intent to open the url in browser then its start downloading the pdf file.
Intent ii=new Intent(Intent.ACTION_VIEW);
ii.setDataAndType(Uri.parse(pdf),"text/html");
try {
startActivity(ii);
}catch (ActivityNotFoundException e)
{
Toast.makeText(QuranPara.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
here is the complete url https://docs.google.com/gview?embedded=true&url=www.elibrary.alnoorpk.com/PDF%20Books/Lafzi_aur_bamahawrah_terjma_2015/Para%201%20(Lafzi%20aur%20Bamuhawra%20Tarjma).pdf
check the image
To show it in WebView try something like this
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
To show it in browser try something like this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
If your files are in assets first copy them to the external storage. Hope that you have handled permission flow.
Try the following code for offline
File pdfFile = new File(Environment.getExternalStorageDirectory(),"pdfName.pdf");//Pdf file path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
} else {
Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
}
Try the following for the webview option if you choose that
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
But Remember if you use google docs for viewing the pdf or any other doc, google docs may throw you the error stating
You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format..... So doesn't seem reliable
So, be cautious with google doc viewing
I need to open a PDF file which I get from URL from JSON.
This URL does not contain file extension at the end of URL. I tried something like this:
final String pdfUrl = dataUrl.replace("data", "file") + "/x?filefield=" + tf.getFieldName() + "&context=\"" + dataItemIdModOne + "\"&skipcache=1";
SharedPreferences sharedPrefs = getActivity().getSharedPreferences(Constants.userDetails, MODE_PRIVATE);
String username = sharedPrefs.getString(Constants.user, "");
String password = sharedPrefs.getString(Constants.pass, "");
String userPassword = username + ":" + password;
final String basicAuth = "Basic " + new String(new Base64().encode(userPassword.getBytes()));
final Map<String, String> extraHeaders = new HashMap<>();
extraHeaders.put("Authorization", basicAuth);
pdfIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse(pdfUrl);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri,"*/*");
Bundle bundle = new Bundle();
bundle.putString("Authorization", basicAuth);
target.putExtra(Browser.EXTRA_HEADERS, bundle);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Please install PDF app", Toast.LENGTH_SHORT).show();
}
}
});
I just need to present to a user an app chooser so he/she will choose with witch app to open a file, or at best, just to directly open available PDF reader on his/her device.
With this implementation, I get the app chooser, and when I try to open a file it loads, but at the end it says that it can not be opened.
So the drill with this URL is that when a URL is called it redirects to another URL which contain the PDF file itself. But I cannot see that second URL. Also when I paste the URL in browser it asks for username and password (that's why I need Basic auth).
Thanks in advance.
Few, if any, PDF viewers will support http/https for their ACTION_VIEW activities. Virtually none of them will support random Intent extras, such as EXTRA_HEADERS.
Download the PDF yourself, using your favorite HTTP client API (e.g., HttpUrlConnection, OkHttp). Then, use ACTION_VIEW to view your downloaded PDF.
I am having an issue, I have never had problem opening files via ACTION_VIEW the next way:
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
String dataType = "image/*";
if (file.exists()) {
Intent fileIntent = new Intent(Intent.ACTION_VIEW);
fileIntent.setDataAndType(Uri.fromFile(file), dataType);
fileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(fileIntent, "Open file");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "There is a problem when opening the file");
}
} else {
Toast.makeText(getContext(), "Invalido", Toast.LENGTH_LONG).show();
}
The problem I am having right now is that even though the file exists when I choose the app to open the file it immediately closes and tells me Not found. I have put the image I am loading in an image view and there is no problem, so the file is valid but for some reason it has conflicts when I am opening it via intent.
I am aware that it may have something to do with the way I am creating the file, I am retrieving it from Google drive so I am writing the file using the Apache Commons library the next way:
DriveContents contents = result.getDriveContents();
InputStream inputStream = contents.getInputStream();
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
try {
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
What is it I am doing wrong? I am not totally sure if the problem has to do with the copy method executing asynchronously or something like that.
Thanks in advance.
I have never had problem opening files via ACTION_VIEW the next way
That code will never work, as third-party apps have no rights to work with files on getFilesDir() of your app.
What is it I am doing wrong?
You are attempting to serve an inaccessible file to third-party programs. Use FileProvider to serve the file, using FileProvider.getUriForFile() to get the Uri to use in your ACTION_VIEW Intent.
I'm creating an app that downloads a lot of pictures from a website and stores them all in the cache folder so that it won't take up too much space on the phone.
Now my problem is that I want the user to be able to click on an image and it will load up the image in the default android picture viewer. I've researched and figured out how to do that no problem, but I'm not 100% sure if this method will work. I can call the Intent no problem and the Pictureviewer opens but it doesn't display the images?
Can anyone let me know if this is possible to do this way? Thanks
Here is the code calling the intent and getting the directory and files...
URL url = null;
try
{
url = new URL(assetsToFullScreen[arg2]);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
File cacheDir = SingleArticle.this.getCacheDir();
String fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File file = new File(cacheDir, fileName);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file),"image/png");
startActivity(i);*/
I am writing an Android application to display pdf files on the device. And I need to use the current versioncode (35498) of the Adobe Reader to display the pdf files.I have with code to display list of files on the screen. Now I need to invoke the Adobe reader (not any other pdf reader installed on the device) onclick of each document. I am not sure how I code that. I am an Android newbie. Any help will be greatly appreciated.
Thanks in Advance,
Navin
I see that you want to open Adobe specifically, but you may want to consider doing it the more Android-like way of opening a general intent and allowing the user to choose how it opens. For your reference, you'd do that with the following code:
private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
startActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}
If you really need to use both Abode Reader specifically, and a specific version, you would need to query for it using PackageManager.getPackageInfo(String, int)
Try the following code
private void loadDocInReader(String doc)
throws ActivityNotFoundException, Exception {
try {
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.parse(doc), "application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException activityNotFoundException) {
activityNotFoundException.printStackTrace();
throw activityNotFoundException;
} catch (Exception otherException) {
otherException.printStackTrace();
throw otherException;
}
}
If you are in "online mode", here is an interesting alternate solution using Google docs.
String myPDFURL = "http://{link of your pdf file}";
String link;
try {
link = "http://docs.google.com/viewer?url="
+ URLEncoder.encode(myPDFURL, "UTF-8")
+ "&embedded=true";
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
This works, setDataAndType method cannot seem to correctly recognize the PDF type if used via URL.
private static Intent newPDFLinkIntent(String url) {
Uri pdfURL = Uri.parse(url);
Intent pdfDownloadIntent = new Intent(Intent.ACTION_VIEW, pdfURL);
pdfDownloadIntent.setType("application/pdf");
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return pdfDownloadIntent ;
}
Unfortunately, the PDF applications I'm using don't anticipate downloading and caching the online content (some will have memory leak error, some will reject link downloading), so you'll eventually end up invoking an intent that downloads the PDF first, before opening the downloaded content via the notification link. I eventually used the solution below:
private static Intent newPDFLinkIntent(String url) {
Intent pdfDownloadIntent = null;
try {
pdfDownloadIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
} catch (URISyntaxException e) {
Log.e("PDF Link Tag", e.getMessage());
}
return pdfDownloadIntent;
}