How to use html string to open browser in android - 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()

Related

How do I open a pdf with content URI on android >= 8?

I am trying to open a pdf with existing installed pdf apps such as Google PDF Viewer. But the PDF screen shows blank.
I created an intent and used ACTION_VIEW filter but when I open in Google PDF it shows just blank screen nothing else even name of the file in Google PDF is not visible only document id (content-provider://..../document/122 <- this id) is shown
String filePath = "content://com.android.providers.downloads.documents/document/346";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mActivity.startActivity(Intent.createChooser(intent, "select an app to open this file"));
setFlags will replace all previous flags on intent. So use addFlags instead -
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
also check if you have long term access to content URI. After Android 4.3 you'll need to ask for persistent permission using takePersistableUriPermission() on content uri you get. Also you'll need to change ACTION_VIEW to ACTION_OPEN_DOCUMENT where you are getting the URI you mentioned for takePersistableUriPermission() to work.
Read below articles for more clarification -
- About Using ACTION_OPEN_DOCUMENT
- How to use new Storage Access Framework to access/modify/create a file

video file cant play with Action_View uri got from Document file on removeable Storage

I could just get my files on removable storage by action_open_document tree and it gives me a document file. I can not open this file with Action_view.
The URI from document file did work!
I can open image file with action view .
String uri=DocumentFile.getUri.toString;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
intent.setDataAndType(Uri.parse(uri), "video/mp4");
activity.startActivity(intent);
Other apps do not have access to that content. Add FLAG_GRANT_READ_URI_PERMISSION to the Intent to pass along your own read access to the third-party app.

How to open the gallery using the file path in android

I have designed two buttons.One button for selecting the file and another button for to open the selected file.I have selected the file correctly and the file path also retrieved.But i cant open the particular file directly by the file path.Any one I have tried something like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ selectedFilePath);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
First, ACTION_GET_CONTENT does not accept a Uri as input.
Second, ACTION_GET_CONTENT has nothing to do with opening a file. Presumably, you should be using ACTION_VIEW.
Third, the value that you are passing to Uri.parse() is not a String form of a Uri.
Also, I would expect few Android devices to have an ACTION_VIEW activity for text/csv content.

How to use startActivity to open data uri

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.

Android : Download file without .pfd, .png... at end of the link

I try to download a file through the browser. it's working perfectly if a have that :
String pdfUrl = "www.myLink.com/document/test.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pdfUrl));
startActivity(intent);
However if the link is :
String pdfUrl = "www.myLink.com/document/test/";
It's very odd because it works on my browser "Chrome". I can download the 2 files....
Using the URL without the full file name will not work, because the Intent system does not know the type of the resource behind the URI - it will probably launch a browser pointed to that address instead of a PDF viewer. What you could do, however, is explicitly specify the content type behind your URL by using intent.setDataAndType() instead of intent.setData(), like this:
String pdfUrl = "www.myLink.com/document/test/";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(pdfUrl), "application/pdf");
startActivity(intent);

Categories

Resources