Launch Microsoft Word from app - java

I am trying to use the Microsoft word app available for android but cant seem to find the Intent options documented
I have a file and wish to open with Word:
private void editfile(final String file, final String field) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Uri uri = Uri.parse(file);
intent.setDataAndType(uri,"appliction/*");
activity.startActivity(intent);
}
This launches Word, but doesn't open the selected file - in fact all the files in my storage are greyed out and not selectable (but can be selected if I run Word outside my app)
Is there an interface guide? anyone with any experience of using?

Related

How to open a directory from intent in android?

I am trying to open a directory with the help of an intent to show the user what is the content inside that folder but I am unable to do so,
but I don't know why the folder won't open and I get this "Can't use this folder" on the file manager.
open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + "XYZ";
Uri uri = Uri.parse(path);
Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_SHORT).show();
openDirectory(uri);
}
});
The method
public void openDirectory(Uri uriToLoad){
// Choose a directory using the system's file picker.
int result = 1;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
startActivityForResult(intent, result);
}
I want to open the folder XYZ after clicking on the button
I am trying to open a directory with the help of an intent to show the user what is the content inside that folder
There is no standard Intent action for that, sorry. Your code is trying to let the user select a document tree.
It is also doing that incorrectly, as EXTRA_INITIAL_URI does not take a file:// Uri as a value. That needs to be some Uri that you obtained previously from the Storage Access Framework, such as via some past ACTION_OPEN_DOCUMENT_TREE request.
I don't know why the folder won't open and I get this "Can't use this folder" on the file manager
From Android's standpoint, your EXTRA_INITIAL_URI value is little better than a random string.
But the user won't know where actually XYZ folder is
Then perhaps you should have let the user choose the location in the first place, rather than forcing a particular location. For example, you could use ACTION_CREATE_DOCUMENT to let the user decide where to place the document on the user's device (or the user's cloud storage, the user's network file server, etc.).
As far as I know using Intent you can browse, open and create files, that shared for all apps by the system. In this case for get file path to open, you can use next code like this (pardon for my Kotlin):
private var launcherForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data.also { uri -> filePath = uri.toString() }
}
}
private fun getFilePath() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "*/*"
addCategory(Intent.CATEGORY_OPENABLE)
}
launcherForResult.launch(intent)
}
As for the directory, it usually opens the last one that was opened before.
If you don't want other applications to see your files, store them in your application's private directory

How to open specific tiktok video link in Android?

I basically want my button to open tiktok video link (link should be opened by tiktok app, not default browser or webview) on click. How to program that button in Android Studio?
maybe a late answer, but what you can do for not only TikTok, but any other app you want to open it from your Android application is to use the package name of it and open it using intent normally, example code to open Instagram for example.
url = "https://instagram.com/p/imagecode"
Uri uri = Uri.parse(url);
Intent likeIng = new Intent(Intent.ACTION_VIEW, URI);
likeIng.setPackage("com.instagram.android"); // -> here is the part
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
//catch the Error if the app not installed.
}
you can get the package name from Google play URL itself it contains the package name, for TikTok, the package name is com.ss.android.ugc.trill so you can use it to open it using the above code, I did not test it with TikTok, but it's tested with Instagram, so I think the same logic applies.

How to open a file in external storage from an application? (Android)

I've added a button to my application which is supposed to open the download folder of the phone, and from there you should be able to click on files that were stored there, from the same app. Right now im saving some data there.
Problem is; I cant open the saved files in the folder.
I can see the files stored right there, but when I press one of them you immediatley go back to the app and not the file that you pressed.
Is there something I'm missing? Are you not supposed to open files stored in external storage from another app?
I've tried adding permissions in manifest and checkSelfpermission for checks in runtime, but with no success.
Here's the button for opening download folder:
private void openSavedLocation(){
if (ContextCompat.checkSelfPermission(ExportAndImport.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(ExportAndImport.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
intent.setDataAndType(uri, "text/xml");
startActivity(Intent.createChooser(intent, "Open Folder"));}
I can open the file perfectly when Im opening it outside the app, not via this "createChooser". What could i be missing?
Any help is appreciated.
but when I press one of them you immediatley go back to the app and not the file that you pressed
That is what your code does. ACTION_GET_CONTENT says "let the user pick a piece of content". It does not say "open that piece of content in some other app". There is no single Intent action for saying "let the user pick a piece of content, then open that piece of content in some other app".
Is there something I'm missing?
If you want to try to open the XML in some other app:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), create an ACTION_VIEW Intent wrapped around the Uri that you get from the Intent passed into onActivityResult(), and call startActivity() on the ACTION_VIEW Intent
If, instead, your objective is to open this XML in your app, you would:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), get the Uri of the content from the Intent passed into onActivityResult(), then use ContentResolver to do something useful with that Uri (e.g., openInputStream() to read in the content)
Here's the button for opening download folder
ACTION_GET_CONTENT uses the MIME type. It will not necessarily honor your supplied starting Uri.

Android open external IMDB application from my app

I have a button in my application which opens the imdb application in the phone with a imdb id I received from https://developers.themoviedb.org/3/getting-started/introduction
But I couldnt find anyway(using intents) to make my app recognize the imdb app and open it and if imdb app do not exist then I want to open the web site. How can I accomplish this?
I think I may be able to point you in the right direction. Just to be sure, you seem to be using TMDB but wish to open in the IMDB app?
The code below is from the Android documentation.
It will start your intent if the package manager can find an app with the appropriate intent filter installed on your device. If multiple apps are able to open this intent then an app chooser should pop up, unless the user has previously set a default for this kind of URI.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
If you add an else onto that then you can use a view intent like this :
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieUrl));
//Watch out here , There is a URI and Uri class!!!!
if (internetIntent.resolveActivity(getPackageManager()) != null){
startActivity(internetIntent);
}
I also found this (rather old) post about calling an explicit imdb uri
Imdb description
startActivity(android.intent.action.VIEW, imdb:///title/<titleID>);
// take note of the Uri scheme "imdb"
I hope this helps. If you post some more detail , code, links , I might be able to work through this with you.
If my answer is way off base then please be kind and set me right. We are all learning every day!
Good Luck.

Open local html file in Google Glass browser

I have an HTML file in my res/raw that I want to view in the Google glass browser.
This answer suggests either copying to shared storage or using a WebView.
I cant seem to make the WebView scroll using the touchpad in the same way the browser does, which renders it useless for my purposes.
I tried copying the file to shared storage (i.e. /mnt/sdcard/Android/data/my.namespace/files/page.html) and using
Intent i = new Intent(Intent.ACTION_VIEW);
File file = new File(f.getAbsolutePath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
i.setDataAndType(Uri.fromFile(file),mimetype);
but I get this exception:
03-18 17:58:53.338: E/AndroidRuntime(5315): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Android/data/my.namespace/files/page.html typ=text/html }
I've checked with adb shell and the file is at that location. Any ideas why this may not be working? Is it a glass specific issue?
Thanks
since you have this error message: "ActivityNotFoundException: No Activity found to handle Intent", you need to define what activity must open your intent, in this case the browser.
Intent i = new Intent(Intent.ACTION_VIEW);
File file = new File(f.getAbsolutePath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
//you need define this activity to open your html file.
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
//intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i.setDataAndType(Uri.fromFile(file),mimetype);
EDIT:
The activity to start the browser in glass is:
i.setClassName("com.google.glass.browser", "com.google.glass.browser.WebBrowserActivity")

Categories