I am trying to view a PDF file remotely located from my device/the emulator. I have been doing quite a lot of research and been looking around stackoverflow on how to do this without having to download the PDF and then viewing the file that way.
This is the relevant code snippet for trying to do this:
if (url.contains("CreateQuoteDocument")) {
webview.getSettings().setJavaScriptEnabled(true);
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url),"application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e("something went wrong", e.getMessage());
}
}
I am getting the following exception:
E/something went wrong: No Activity found to handle Intent { act=android.intent.action.VIEW ...
So having this said, I have two questions,
Why isn't this working?
Is there another way of doing this? (while not using google docs)
EDIT
The URL is properly formatted with http://www
Try this:
String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
Log.i(TAG, "Opening PDF: " + url);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
Why isn't this working?
You have not installed an app that contains an activity that supports that Intent structure (e.g., application/pdf for whatever scheme happens to be used by url).
Is there another way of doing this?
You could install an app that contains an activity that supports that Intent structure. Of course, not all users will have such an app, but perhaps you are only intending to use your code personally, in which case there are no other users besides you.
Related
I'm currently working on making an Android app, but have been having some trouble. I want to be able to push a button with the title of the document as it's TextView and then have that document open to be read. I've looked around for guides but everything I've found is either out of date or doesn't explain any of the code shown. Does anyone know how I can put such a thing in my app? At this point, I'm not even sure where to start with the process.
Note, I'm working in Java not Kotlin.
UPDATE: I was directed to a solution using intents. Now there seems to be an issue loading the file. My code is this:
public class atotf_pdf extends literature {
File file = new File("/storage/emulated/0/AToTF Preview.pdf");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Uri pdfURI = FileProvider.getUriForFile(atotf_pdf.this, "net.whispwriting.whispwriting.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfURI, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
Is it something here that's causing the error?
It looks like the file is somehow not being stored in the filesystem when the app is installed.
Just like #ruben said, you could use a WebView for such a task: https://developer.android.com/reference/android/webkit/WebView. For more information about using WebView to display content, you can take a look at this post: How can I display a pdf document into a Webview?
Another potential solution could be using intents, which has also been discussed before: How to open a PDF via Intent from SD card
Hopefully this helps!
I made a QR reader APP for my school project, the app works very well but it has a little mistake. When I scan a QR code the app just shows me the text. However, when I created a QR code linked to "www.google.com" (a simple link), my app just shows me "www.google.com" and doesn't open it in the browser.
I use this video to make my app : https://youtu.be/Fe7F4Jx7rwo
He is a nice guy He said : "use intent to open it in a browser"
But as I said in previous posts : "in my school my teacher prefer to teach Visual Basic instead Java or C++" ... So I'm a 0 in Java or C++
Can anyone suggest what to do?
I suggest the following. When you get the text from the QR call the following code to either open the browser or show the text on the screen (your current implementation):
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
String text = resultCode.getContents();
if (Patterns.WEB_URL.matcher(text).matches()) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(text));
startActivity(browserIntent);
} else {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}
I have implemented sharing feature using Intents and it's working properly. I'm sharing a bitmap by converting it to a file. But I want to add an extra caption along with the post when users share it. I tried this:
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_TEXT, " Shared via App"); //this part doesnt work
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile));
//sharefile is my image file. It gets shared properly.
try {
startActivity(Intent.createChooser(share, "Share Product"));
} catch (Exception e) {
}
But it doesn't share the CAPTION. It shares the Image successfully.
Try Intent.ACTION_SEND_MULTIPLE instead Intent.ACTION_SEND
Other applications determine whether or not they support attaching a caption to an image or not. If you are experiencing issues with a specific platform ignoring the Intent.EXTRA_TEXT that you pass with the image, you will need to contact the developers of that application for support.
Facebook is one example of a platform that does not support attaching a caption or description to an image. Here's a bug report requesting this feature with the response from the Facebook team. In this case, attaching a message would violate Facebook's platform policies.
Just add this line:
intent.putExtra(Intent.EXTRA_TEXT, caption);
Ok, so this code works great to call the stock android camera app:
public void clickEvent(View e) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
However, I need to capture that picture file after it's taken, rename it and maybe place it somewhere else.
Is there a way to do that?
You can set the EXTRA_OUTPUT extra on the intent to tell whatever app responds to the intent where to put the file.
How to launch specific intent (such as call) using google voice? How to pass phone number using intent? Following code launches google voice but what value to be passed for making call using google voice as intent extras?
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.apps.googlevoice", "com.google.android.apps.googlevoice.activity.conversationlist.ConversationListActivity"));
intent.putExtra("label", "<phone number>");
startActivity(intent);
Here what should i put in label to start the intent that launches a call using google voice?
Any help is appreciated... Thanks in Advance...
NEVER target applications directly like that UNLESS it is in your package. You should be using the Intent filter to catch that particular application. Sometimes you have to target an application like this, but this brings up the risk of change in package name errors.
To handle your particular application, you need to look at how information is being passed into Google voice. this will give you insight and how to target it WITHOUT targeting the exact package name.
What #JoxTraex said makes sense. However some clients need funny features like this, so we have no way but to implement this:
try {
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + mobile));
intent.setPackage("com.google.android.apps.googlevoice");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
GMHintManager.getInstance().showError(context, "Google Voice not installed");
}
Yes, you should try-catch ActivityNotFoundException.