Search code examples
androidandroid-activitydata-uri

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?


Solution

  • 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.