How to use startActivity to open data uri - android

I want to open a data: url containing a pdf in an activity in my android application. I have something like the following code:
String url = "data:application/pdf;base64,JVBERi0xLjIgDQol4uPP0w0KIA..."; // shortened for brevity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
I am seeing an error:
error opening uri: No Activity found to handle Intent {
act=android.intent.action.VIEW dat=data:application/pdf... (followed
by the rest of the data url).
How can I resolve this?

Since approximately zero apps in existence will support that scheme, you will need to decode the PDF yourself, write it to a file, and then open the PDF viewer on the file.

You need to have an app that can handle opening pdf documents. If you still wish to do this catch an ActivityNotFoundException and tell the user to download one.

Related

How to use html string to open browser in android

I want to open the phone's browser to show my html string.
Now my codes are:
Uri uri = Uri.parse(htmlString);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(browserIntent);
But it meets an error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=...
where dat is my html string.
So how can I correctly open the browser?
Thank you.
Uri uri = Uri.parse(htmlString);
HTML is not a Uri.
So how can I correctly open the browser?
Step #1: Write the HTML to a file on internal storage (e.g., getCacheDir())
Step #2: Set up FileProvider to serve files from the directory that you used in step #1
Step #3: Use FileProvider.getUriForFile() to get a Uri for your File that you used in step #1
Step #4: Use that Uri in your Intent, also adding FLAG_GRANT_READ_URI_PERMISSION to the Intent before calling startActivity()

PDF rendering in android

I am newbie to android and development world. I need help to solve this problem.(scenario as follows)
I have created one android app using HTML5 and CSS and bundle it out using phonegap.
Now I need to display PDF file in this app with two options for user whether first download and then read or second read online
So My problem is, I am not able to dispaly PDF in app.
How could I achieve this? please help . I am trying to solve it from last 4 days and tried out each and solution which is already given in forum , but still no success.
It would be great for me if someone ans step by step procedure or one example to refer.
Thank You
Minal
This may help: How to open a PDF via Intent from SD card
The basic idea is to get a pdf app to render the pdf for you, and your app just kicks off an intent to do that.
try this code
private void openBook() {
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
}
you can open pdf file via intent like this
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

Play Video on the Browser

I tried to open the video from url with Intent.ACTION_VIEW, and it was successful.
But, the problem is, after the browser appeared for a second and it's automatically minimize and the front is the activity on the apps.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("the url of the video with format .m3u8");
intent.setData(data);
startActivity(intent);
I'm expecting exactly the same way when i put the URL into the browser manually, but without minimize the browser when it has already been launched.
Need some flags? grateful for any response and suggestions.
I bet that the browser is just quitting as a result of an inability to display the content. If you plug in the video url to the browser manually, does it work? What is the filetype of the content you are trying to display?
Have you tried explicitly setting data type by using setDataAndType(Uri data, String type) where mime type is "audio/x-mpegurl" (the full mime type is "audio/x-mpegurl; charset=UTF-8"). You might have some switch statement to check URL user has clicked to force type in case link points to ".m3u8".

Android - Call default browser with and redirect to a designated url

Hi
I want to write an app to call default browser and redirect to a designated url.
Any suggestion to 1)call the default browser, 2)redirect to a designated url.
Thanks
you just want to launch an ACTION_VIEW intent with the Uri of the webpage as your data element :
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("http://www.bbc.co.uk"));
startActivity(httpIntent);
To open the default browser use an Intent with the action VIEW. To tell the browser which page to load us the data-part of the Intent.
Example:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(browse);
Since this is a basic task in Android you might want to read some basics about Intents in Android.

Starting a browser from a textbox via Intent - Is http:// required?

I have the following code:
/** Open a browser on the URL specified in the text box */
private void openBrowser() {
Uri uri = Uri.parse(urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
When I input "http://www.google.com" to the textbox, it works fine. However, when I try something like "www.google.com" it crashes with:
No Activity found to handle Intent {
act=android.intent.action.VIEW
dat=www.google.com }
Am I using Uri wrong? Is there a way to extract full address from it? Or am I supposed to write code that adds http manually? e.g, if not starts with http://, add http://.
Thanks!
Am I using Uri wrong?
No. However, www.google.com is not a valid uniform resource identifier.
Or am I supposed to write code that
adds http manually?
Yes.

Categories

Resources