I am having webservice which has a String URL response, URL could be of .PNG and .PDF I don't want to download image and pdf files from that url I want to show them on default viewer i.e. if i click to view image button then it should open image url to any default photo viewer and if i click to view pdf button then it should open pdf in default pdf viewer.
Please help Thanks in advance.
File file = new File(url);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String extension = file.getName().substring(file.getName().indexOf(".") + 1).toLowerCase();
String mimeType = myMime.getMimeTypeFromExtension(extension);
newIntent.setDataAndType(Uri.fromFile(file), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MasterPlan_Activity.this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
The basic concept here is opening an app that can handle your resource (JPEG or PDF). So, you need to request an Implicit Intent with the data URI that needs to be shown.
You can use intent with specific filter to achieve that, as shown below
Image:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
Pdf:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
or
private static final String googleDocsUrl = "http://docs.google.com/viewer?url=";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl + url), "text/html")
Related
How to open Instagram app in posting mode (so the same view we get after clicking plus at home screen within Instagram app)? I've seen android apps which are doing that, however wasn't able to find information explaing how to achieve it.
That code works for opening the home view:
Uri uri = Uri.parse("http://instagram.com/");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
likeIng.setPackage("com.instagram.android");
startActivity(likeIng);
Perhaps there is some other uri which could work?
After a bit of research it turned out that deep linking with 'instagram://share' opens the desired view.
You can do that only when you have a media to share on Instagram:
try {
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.instagram.android");
share.setType(type);
File media = new File(mediaPath);
Uri uri = null;
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(share);
} catch (Exception e) {
e.printStackTrace();
}
You can create a fake media though to be able to use this method.
Just to expand on #Paavo answer to show the answer in code.
Uri uri = Uri.parse("instagram://share");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.instagram.android");
//Check that the user can open the intent, meaning they have the Instagram app installed
if (getPackageManager().resolveActivity(intent, 0) != null)
startActivity(intent);
else //User doesn't have app installed, handle however you want
throw new Resources.NotFoundException();
What I want to achieve is to display PDFs directly in browser.
private void openPDF(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
With this method the PDF is downloaded, but I have to manually open it in order to see it.
I remember this method worked just fine a while a go, did something change in the intent or chrome maybe?
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
If this can help.
What do I want to achieve? I want to choose pdf or doc file and show them on recyclerview. What have I done?
Firstly, I tried to start activity like below:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Open document"), DOCUMENT_REQUEST_CODE);
When this attached document is clicked, I do following:
Intent docIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.providers.downloads.documents/document/doc_name"));
docIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(docIntent);
Above method opens Google Docs to load document and it can open only docs located on Google Drive. There is black view when I try to open document located in device storage. URI of document located in device is : content://com.android.providers.downloads.documents/document/doc_name and URI of document located in Google Drive is like : content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D430
After some searching I changed click method to following:
File file = new File("content://com.android.providers.downloads.documents/document/doc_name"); // url here is string type
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf"); //only pdf here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
But in above method, exists() returns false. Note, that all url that I pass are string type. So, how can I solve my problem?
I'm making a browser android application.
How do you determine if the url is a file? Like when you click a download button. Currently webView just loads and does nothing.
Do I need to check if the url ends with an extension and open it through intents? or is there another way?
You can do as following:
1) Check if the url is file:
if(URLUtil.isFileUrl(file)){
getExtention(url);
}
2) Get the extension:
public String getExtention(String url) {
String filenameArray[] = url.split("\\.");
String extension = filenameArray[filenameArray.length-1];
return extension;
}
Trigger intent according to the extension:
if(getExtention(url).equals("jpg")){
openGallery(url);
}
else if(getExtention(url).equals("pdf")){
openPDF(url);
}
3.a) OpenGallery() as ex:
public void openGallery(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
startActivity(intent);
}
3.b) Open Pdf
public void openPdf(String url){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
Hope this helps.
I know that I can let user view their PDF file like that
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
startActivity(intent);
But I was wondering, if I dont know the file type (could be an image, pdf, text ..etc). Is there away I can let the user open it (and android automatically detect the file type and show the list of possible programs)?
Thank you
Try this,
import android.webkit.MimeTypeMap;
try
{
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File(aFile.getAbsolutePath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
myIntent.setDataAndType(Uri.fromFile(file), mimetype);
startActivity(myIntent);
}
catch (Exception e)
{
// TODO: handle exception
String data = e.getMessage();
}