How to open a selected file in android? - android

In windows text files are displayed with notepad icon. When we double click the particular file it open’s the notepad and displays the file.
Like that I need to open the file from the download folder in android. I have used the intent-filter for register my ics file’s mime type. When I select the file in the download folder it just opens my application only. At that time I need to open / read the selected file. How to do this? I am new to android Can anyone help me?

You can determine the Intent your activity was fired with by calling getIntent(), and you can access the data for that Intent (in this case, likely a URI), by calling getIntent().getData(). Also, the action is likely ACTION_VIEW, but you probably know this already if you've set up your <intent-filter> properly.

When your application is called, it is supplied an Intent.
You can get the file name from the intent that is supplied to your application and open the file using new FileInputStream(String filename).
You can now read the data from this stream as you normally would in Java.
P.S. Make sure your application has enough permissions to access the file. You may need to declare the required permissions in your manifest.

Related

Register new file type/extension on app install/open

Let me first say I've been reading some similar questions here, for example this one, but was only able to solve my issue partially and still having doubts.
I'm not sure how custom file registration works in Android, I don't know if a new file type can be registered on app install or it can only be done with an intent-filter in the activity that will open the file, and second, I'm not quite sure if there is any possibility to automatically associate your own custom file with your app, or this is something that only the user can do.
That said, I'm creating a custom file type (.aw extension) to be opened with my app and with the next intent-filter I can open the file from the file explorer -for example- in my Android 9 Huawei, but it's not working at all with other devices, like my old Android 5 tablet (which says "Cannot open file" on file click) or my friend Android 9 Samsung. I said that solved "partially" because it appears not to be a universal working intent-filter.
How can I find a "all Android versions" working way of registering a custom file type and let me know if there is any possibility that registration is automatically done so whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file.
This is my intent-filter in manifest.xml
<intent-filter
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="*"
android:mimeType="application/aw"
android:scheme="content" />
</intent-filter>
And this is how I treat it in activity that will open the file:
private String[] getIntentExtras()
{
Intent intent = getIntent();
String fileContents = null;
String action = intent.getAction();
if(action!=null)
{
if (action.equals(Intent.ACTION_VIEW))
{
fileContents = AWImport.importData(intent);
if (fileContents.startsWith("whatever"))
{
fileContents = fileContents.replace("whatever", "");
}
else
{
showErrorOpeningFile();
return null;
}
}
}
else
...
I'm not sure how custom file registration works in Android
It has never worked especially well, and it is nearly pointless on modern versions of Android. Make sure that you have other ways for the user to choose content for use in your app, such as an "open" toolbar button that triggers an ACTION_OPEN_DOCUMENT Intent.
I don't know if a new file type can be registered on app install or it can only be done with an intent-filter in the activity that will open the file
It can only be done with an <intent-filter>. That typically goes on the <activity> that will handle the Intent. It could go on an <activity-alias> that you enable/disable separately, if there is some need for this.
I'm not quite sure if there is any possibility to autommatically associate your own custom file with your app, or this is something that only the user can do
Having the <intent-filter> is automatic. Few apps will be creating a matching Intent, though.
I can open the file from the file explorer -for example- in my Android 9 Huawei
Presumably, that is a bug in that device. Approximately zero apps written in human history will know about your unofficial (and arguably invalid) application/aw MIME type to create an Intent that uses it.
How can I find a "all Android versions" working way of registering a custom file type
There is none.
whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file
There is no guaranteed way to do this. How a file explorer handles files is up to the developers of the file explorer, not you. Many will only work for common and popular MIME types, not others.
You are welcome to add an <intent-filter> using pathPattern to try to match literally on file extensions. Since a Uri does not have to have a file extension, this will not work in all cases, but it will work for some users and some file explorers.
In general, consider this form of getting content to your app to be an additional feature, usable by a small percentage of your users. Focus primarily on something else, such as ACTION_OPEN_DOCUMENT, that will work for everyone.

Why does an "Intent Selector" exists, what is it used for?

Android's Intent class provides an API called setSelector. I am trying to understand it from the example given in the documentation.
I want to ask that why did Android need to add this API ? What was breaking in Intent before this API ?
My understanding from reading the references is that the problem this API is intending to solve is where you want to send a launcher intent for an app that meets some general restrictions. Say you want to match all apps that open .mp3 files, but you don't want to actually open an mp3 file, you just want to launch an app that supports that. In that case, you could create a generic ACTION_MAIN, CATEGORY_LAUNCHER intent, and set the selector to an intent with an mp3 mime type or data URI.
Before this API there would be no way to do that - if you wanted to target an app that supports opening mp3s, you would have to send an intent for an mp3, which could either cause music to start playing, or cause the music player to throw an error. Also, depending on the music player's launch mode, the launcher intent may return to an existing instance of the music player, while the mp3 intent might create a new one.
According to my understanding, it gives choice to user which intent he wants to select. In that documentation they have given that it gives selection of intents whether user wants to open app's main activity or wants to launch any diff app/activity other than user's app. This is what i understood from that documentation. Check this links for your reference : https://code.google.com/p/android/issues/detail?id=67162 & http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/content/Intent.java#Intent.setSelector%28android.content.Intent%29

How to lauch android's apps preferences (from Contacts, Messages, ...)

I've been searching and I couldn't find a topic that could clarify me 100%.
My question is: How can I launch an android's app preferences activity (from Contacts, Messages, ...) on my own app?
I give an example:
Imagine I'm developing an app which allows the user to quickly access to Message's Settings. I don't need to send or receive any information, I only need to open the activity, create a shortcut for it.
Someone knows if this can be done and even opening specific locations of the apps?
You don't need to know any specific locations or specific apps for these actions, simply look into Intent.ACTION_PICK.
Picking a Contact: get contact info from android contact picker
Picking a picture: How to pick an image from gallery (SD Card) for my app?
The best answer in this thread has the solution:
android: how do i open another app from my app?
Also check:
http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
To open settings, you can try:
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS, 0));
There is no difference in writing in code an Explicit Intent which will go to a specific activity of your app and using the same format to go to a specific activity some other app. A few things to be aware of: (1) The receiving activity may be expecting particular data and fail otherwise. (2) The standard apps you are considering like Contacts, Messages while you can find the source for them in the Android Open Source Project (AOSP) may be changed by the manufacturers so that the activity names and necessary extra data could be different.
In order to maintain as much compatibility between all of the different Android manufacturers, you should stick to the standard implicit intent with the appropriate action/data.

Make your app a default app to open a certain file

How do I make my android app the default application to open a certain file type?
I tried to clear the defaults but the option is greyed out, does anyone know how to do this?
I want to make my app open with my something.whatever files in android.
You cannot force this, and you shouldn't be able to. What you can do is make known your app can open the filetype. The user gets the standard "what program do you wish to use" dialog, and the user can select something as default.
If the user has allready selected a default, he/she needs to undo that before the dialog appears (obviously, otherwise a 'default' wouldn't have a lot of effect, now would it?)
You can tell the world (aka: android) that you want to open a certain file type by adding an intent-filter. What you basically do is put in your manifest that an activity in your app knows how to handle the filetype (or website/protocol etc). That's quite easy to find, a random question here on SO about the same issue would be: Android intent filter for a particular file extension?
This subject has already been covered here: Register to be default app for custom file type
Add it to you intent-filter:
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
</intent-filter>
In Android, how to associate file with a different program.
I'm using a "Droid Maxx" with Android Version 4.4.4
How to make a something.html file open with a file editor instead of firefox after you've associated firefox to always be associated with *.html files.
You have to clear out the default setting for the app that has associated with a filetype.
Do this
Go to "Settings". It's in the app list.
Under Device, click "Apps".
Scroll down and click Firefox.
Under the "Launch by default" entry, click "Clear defaults".
Open your something.html file again.
Once again you are presented with a choice with what kind of program you want to open this file with.
If you want to associate and disassociate file types with apps indivdiaully:
There is a third party app called: Default App Manager Lite:
https://play.google.com/store/apps/details?id=com.appiator.defaultappmanager
Then you can run the program and manage your filetype -> default app associations individually.
I think in android every app/activity must have some intent actions and categories in its manifest file. In this intent settings only you have to make your app as default.
Even you write default in manifest, if any other apps found with same intent settings, android system will display all apps with same settings. user has to select one of them, there only it provide one check box to make one app as default for next time onwards. If user didn't check it the list will be displayed every time user opens the file.
I hope it may help you.
Try this
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(Uri.parse("file://"+path),
"application/vnd.android.package-archive");
startActivity(i);
The path variable is your path.

Starting activity from Android app: view a text file

I'm working with an Android app that generates a text file and stores it on the sdcard.
I would like to open the file in a text editor. The one I have installed is Office Suite Professional.
I have the following code:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse
("file:///sdcard/report.txt"));
startActivity(intent);
When I run this snippet of code, I get the following error:
Sorry! The application XXXX () has stopped unexpectedly. Please try again.
How can I get my app to load OfficeSuite Pro with a specific file?
Thanks
Mike
First, you will want to use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your error.
Had you done that, you would have seen an exception indicating that it could not find an activity to match your Intent.
In this case, you have one definite problem and one possible problem.
The definite problem is that you do not have a MIME type. Use setDataAndType() rather than setData(), with "text/plain" as the 2nd parameter.
The possible problem is that "OfficeSuite Pro" (whatever that is) may or may not know how to view text files.

Categories

Resources