[HELP] 'Open With' File Manager - Java for Android App Development

I'm in the process of creating a file manager, however I've stumbled across a problem regarding opening/executing files. At the moment I'm opening files like this :
Code:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(Intent.createChooser(intent, "Open With"));
But dialog that shows doesn't contain the applications that I know would open the file type and sometimes it doesn't show any 'Open With' dialog, it just tried to open the file using a PDF reader!?
Do I have to manually figure out what applications are compatible with certain files? That would be a massive headache...
Thanks.

http://stackoverflow.com/questions/2916108/android-open-a-pdf-from-my-app-using-the-built-in-pdf-viewer?lq=1
Look down about half way down the page, see if that helps out.

zalez said:
http://stackoverflow.com/questions/2916108/android-open-a-pdf-from-my-app-using-the-built-in-pdf-viewer?lq=1
Look down about half way down the page, see if that helps out.
Click to expand...
Click to collapse
Thanks, but that's not really what I'm looking for. 'build.prop' for example, a text document but because of its extension '.prop' the Intent type gets set to 'null' and when its null Android just opens it using Adobe PDF Reader as default which I DO NOT want. I want it to list the applications that CAN open a text file.

Gotcha. What about:
http://helloworldcodes.blogspot.com/2011/10/android-open-folder-with-default.html
Or
http://indyvision.net/2010/03/android-using-intents-open-files/
First comment on this blog

Thanks for that! Turns out the problem was 'intent.setDataAndType' I wasn't setting any data. I've still had to filter a few extensions though.
Code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String type;
if(selected.getExtension().contentEquals("prop") || selected.getExtension().contentEquals("rc") || selected.getExtension().contentEquals("conf") || selected.getExtension().contentEquals("sh")){
type = "text/plain";
}else{
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(selected.getExtension());
if(type == null)
type = "*/*";
}
intent.setDataAndType(Uri.fromFile(new File(selected.getPath())),type);
startActivity(Intent.createChooser(intent, "Open With"));

Related

Trouble with database

Hello all! I'm somewhat new to Android development, I'm using eclipse to do all of my development. Right now I am working on an "away message text" application. I am having trouble with database query returning a string of the first index of data. Here is my code where i am trying to pull data from the database. This is being done inside my ReceiveText class, which extends BroadcastReceiver.
mDbHelper = new TextsDbAdapter(context);
mDbHelper.open();
getText = mDbHelper.fetchText(0);
message = getText.getString(getText.getColumnIndex(mDbHelper.KEY_BODY)).toString();
The error that I am getting is Cursor index out of bounds...any recommendations? Thanks!
you always start out of bounds.... you have to moveToFirst() first...
Thanks. Would I moveToFirst() before I fetchText()?
you're probably going to have to change how the fetchText method is implemented.
you'll probably want something like
Code:
public String fetchText(int id){
Cursor c = getContentResolver().query( uri,
new String[] { <name_of_text_column> } ,
"_id == " + id, null, null);
String text = null;
if( c != null ){
if( c.moveToFirst() ){
text = c.getString( c.getColumnIndex( <name_of_text_column> ) );
}
c.close();
}
return text;
}
Thanks. The method I have for fetchText returns a Cursor, however I made another method that returns a string, similar to what you recommended. For some reason, there is something wrong with the rowid sending in, I want to just get the first one, if I'm not mistaken, wouldn't it be 0? I have a seperate ReceiveText.java class, which extends Broadcast Receiver. The part where the messages are stored into the database is very similar to androids notepad demo. When a text is received, I want the ReceiveText class to query the database and just pull the first text that was actually stored into the database. I have 3 columns, KEY_ROWID, KEY_TITLE, and KEY_BODY. For some reason there is an issue when trying to query the database from the ReceiveText class. Any ideas that would make better sense or be easier to implement? Any help is much appreciated.
oh, right. I wasn't paying attention and for some reason I thought it was returning a string. I see now that it's obviously a cursor..
what you are doing sounds fine but you sure really look at the database on the phone/emulator.
open up adb and type
Code:
sqlite3 /data/data/<name_of_app>/databases/<name_of_db>
then you can do
Code:
select * from <name_of_table>
personally, I find it way easier to read if you change the mode with a ".mode line" command(the dot is important).
that should give you a good idea of why it is not working properly.
How do I open up the adb? I looked online, and it says i can run it through command line, or terminal since I use ubuntu, but I'm not sure exactly how to open it. Apologies for my noobness with android development i'm trying to learn!
if you're running it on a phone/tablet, you'll have to set up the adb drivers
but if you're just using the emulator, you're fine
Code:
cd /<path_to_sdk>/platform-tools/
./adb devices ##this should list the device. if not you have a problem.
./adb shell
and now you can execute commands on the device, like sqlite3
Awesome, I got that to work, and i can see the three "texts" that i've added to the database...looks like this...
1|Sleeping|Sleeping...text you when I wake up.
2|Driving|Driving right now...text you when I'm done.
3|Xbox|Playing xbox...text you later.
So this means that my database works. The numbers 1,2, and 3 are the KEY_ROWID, sleeping, driving, and xbox are the KEY_TITLE, and the third part is the KEY_BODY.
For some reason the ReceiveText class is having trouble pulling this information from the database, I really think it has something to do with when i Query the database, it asks for a columnindex, and i am putting a 0 as the parameter. Any ideas? Thanks again for all your help. It's a great learning process.
yeah, there doesn't seem to be a column with _id == 0
that is strange. when you defined it did you use "integer primary key autoincrement"? This should start at 0, I believe.. maybe you deleted your first entry at some point?
try it with 1 and see if that works.
Man, thanks so much for your help...it works! Here is how I did it from the ReceiveText class.
mDbHelper = new TextsDbAdapter(context);
mDbHelper.open();
getText = mDbHelper.fetchText(1);
message = getText.getString(2).toString();
getText is a Cursor type, and message is a String type.
I really appreciate the help, I understand a lot more now. My next step is to set the text to use without having to specify from the ReceiveText class...any suggestions? I was thinking adding another column in the database that would hold either a 1 or 0, and if it is selected, it updates the database and changes that field to a 1, then the ReceiveText class will query the database to return the field that has the 1...make sense? lol
Just to clear this up a bit for those still a little fuzzy:
Code:
Cursor c = db.query(TABLE_NAME3, new String[] {NAMESHORT, LAPTIME, LAPNUMBER}, null ,null, null, null, orderBy);
The column indexes are what you have in the "new String[]" area whether you have 1 or 50 items. So, NAMESHORT is index 0, LAPTIME is index 1 and LAPNUMPER is index 2. It's NOT the column number of where it is in the table
Just a personal preference of mine, but I code all this in the database methods and return what I need from there. Seeing hard-coded numbers in a program always bothers me. Instead of returning cursors I'll return a StringBuilder or ArrayList or whatever. Just sayn'

Open Maps and show a place.

Hello, im trying to program an intent for open google maps and show a place.
I want to select the place by coordinates.
I have tested
Code:
String uri = "geo:0,0?q="+address;
context.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
It works fine and show the location of the address with a marker
But using coordinates:
Code:
String uri = "geo:"+ lat+ "," +log;
context.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
Is the zone is shown but there's no marker for showing the exact place.
how can I place the marker in the intent (with a label if possible)
Thanks in advance.
i take it you've seen this http://developer.android.com/guide/appendix/g-app-intents.html
it looks like it says that the use of the geo tag is underdevelopment at the moment. it might just be that the implementation of this uri is broken.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")");
startActivity(intent);

Adding Bookmarks Are Invisible?

So, I wrote this block of code:
Code:
ContentValues values = new ContentValues();
values.put(BookmarkColumns.BOOKMARK, "1");
values.put(BookmarkColumns.CREATED, "1311170108");
values.put(BookmarkColumns.DATE, "1311170708");
values.put(BookmarkColumns.FAVICON, "favicon");
values.put(BookmarkColumns.TITLE, "XDA");
values.put(BookmarkColumns.URL, "http://forum.xda-developers.com");
values.put(BookmarkColumns.VISITS, "1");
getContentResolver().insert(Browser.BOOKMARKS_URI, values);
When executed, I get no errors. Looking in the browser bookmarks section, the book mark is not there. If i click the Most Visited tab, this shows up and also has the yellow bookmark star next to it, indicating that it is a bookmark. If i click the star to unbook mark it, and click it again, then view the Bookmarks tab, it shows up.
If I use another piece of code to print out all the bookmarks found after I add it, mine shows up
Even with rebooting, they are not showing.
So I ask, why is it that they are not showing up in the bookmarks page of the browser?
I have tried everything and looked around everywhere, and nothing
Thanks!
there is no sanctioned way of adding a bookmark without user input. the normal way would be a call to android.provider.Browser.saveBookmark()
Code:
public static final void saveBookmark(Context c, String title, String url) {
Intent i = new Intent(Intent.ACTION_INSERT, Browser.BOOKMARKS_URI);
i.putExtra("title", title);
i.putExtra("url", url);
c.startActivity(i);
}
other than that dealing with the DB directly would be the only way to add one. try to follow that startActivity to the dialog and see if there is an intent sent to the Browser telling it the DB was updated
killersnowman said:
there is no sanctioned way of adding a bookmark without user input. the normal way would be a call to android.provider.Browser.saveBookmark()
Code:
public static final void saveBookmark(Context c, String title, String url) {
Intent i = new Intent(Intent.ACTION_INSERT, Browser.BOOKMARKS_URI);
i.putExtra("title", title);
i.putExtra("url", url);
c.startActivity(i);
}
other than that dealing with the DB directly would be the only way to add one. try to follow that startActivity to the dialog and see if there is an intent sent to the Browser telling it the DB was updated
Click to expand...
Click to collapse
But, even with restarting the device the book marks are not there. Surely the browser updates the database at that point.
But it also dosnt make sense because if the page was shown in the history, it has the bookmarked indicator next to it
I must do all of this in a fully transparent way. Showing that popup for each one will not do :/
there are a few other columns that are interesting. 'user_entered' which can be '0' or '1'
But I think your best bet is to find the dialog that saveBookmark() calls and analyze its src
Here it is addBookmark()
http://www.java2s.com/Open-Source/A...rowser/com/android/browser/Bookmarks.java.htm
It appears to be static. Why not just call this method?
static void addBookmark(Context context, ContentResolver cr, String url, String name, Bitmap thumbnail, boolean retainIcon)
------nvm i think its package restricted
From something awesome

[Q]To create android app for consuming OData services via Netweaver Gateway

Hi,
I need to develop an android application using Eclipse and Netweaver Gateway plugin. When I run my code as a console application, it works fine. However, I'm unable to consume the OData services from the android application. I need to call an OData service, which is hosted on an ECC on an organization's server and display the details and whatever on the application screen (in text-fields or whatever). The "android.permission.INTERNET" is defined in the AndroidManifest.xml, already. See the code below: (Url & credentials have been omitted)
private void readExample() throws RestClientException, ParserException
{
String reqUri = "http://<some url>";
IServerConnectionParameters gateway = new ServerConnectionParameters("<host name>","<port>","<client>",false);
IAuthentication credentials = new UsernamePasswordCredentials("<username>","<password>");
IRestClient restClient = RestClientFactory.createInstance(gateway, credentials,Representation.ATOM);
IRestRequest request = new RestRequest(reqUri);
IRestResponse response = restClient.execute(request);
// Convert the response body XML into an ODataCollection object
ODataEntry collection = ODataUtil.parseODataEntry(response.getBody());
ODataProperty[] properties = collection.getProperties();
for (ODataProperty oDataProperty : properties)
{
Context context = getApplicationContext();
CharSequence text = oDataProperty.getValue();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
This is the function which gets called on a button click event and for example here, the details should be fetched from the service and shown as toast messages. This block works very fine using command line, but I'm unable to consume this via android. The debugger gets stuck on the "IRestResponse response = restClient.execute(request);" line and after a while returns an error of "Connection ETIMEDOUT" and could not connect to the host.
I'm using two reference libraries : com.sap.nw.gateway.odata.client_2.6.400.201211111531.jar, odata4j-0.7.0-clientbundle.jar.
Could somebody please shed some light on this?? I'm deeply stuck and need this urgently!! :crying:
Nobody ready to help, guys?? :'( :'(
bugnotch said:
Nobody ready to help, guys?? :'( :'(
Click to expand...
Click to collapse
Hi,
Please use Gateway Productivity Accelerator (tools.hana.ondemand.com/#gateway) instead of the Netweaver Gateway plugin for Eclipse.
Also, in general, make sure you don't call to a very large amount of data in a single request, but rather use paging or other.
Regards

Android-"Complete Action Using" Ignoring input - What am I doing wrong?

So I'm a noob when comes to android programing. Can someone tell me if the "Complete Action Using" dialog box is suppose to set the defaults when you use the "Always" response? Personally I thought it was however in my programming I have not been able to get it to use the "always" response always. What I mean is if I have "Google Music" installed and then I want to kick off the music player i ALWAYS get the "Complete Action Using" dialog popup.
To remove my code I went back to the Android developers first app program and used that. The SendMessage code now looks like this below. When running this on the emulator the "Complete Action Using" popup always comes up irrespective of selecting "Always" or "Just Once". What have I missed or done wrong?
Code:
public void sendMessage(View view) {
// Do something in response to button
if(android.os.Build.VERSION.SDK_INT>=15){
Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
startActivity(intent);
}else{
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");//Min SDK 8 and deprecated in API 17 for makeMainSelectoryActivity
startActivity(intent);
}
}
tommiyau said:
So I'm a noob when comes to android programing. Can someone tell me if the "Complete Action Using" dialog box is suppose to set the defaults when you use the "Always" response? Personally I thought it was however in my programming I have not been able to get it to use the "always" response always. What I mean is if I have "Google Music" installed and then I want to kick off the music player i ALWAYS get the "Complete Action Using" dialog popup.
To remove my code I went back to the Android developers first app program and used that. The SendMessage code now looks like this below. When running this on the emulator the "Complete Action Using" popup always comes up irrespective of selecting "Always" or "Just Once". What have I missed or done wrong?
Code:
public void sendMessage(View view) {
// Do something in response to button
if(android.os.Build.VERSION.SDK_INT>=15){
Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
startActivity(intent);
}else{
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");//Min SDK 8 and deprecated in API 17 for makeMainSelectoryActivity
startActivity(intent);
}
}
Click to expand...
Click to collapse
If you select "Always", the default application will be set which you can remove from the settings.
However, there should be a better possibility to do this.
You could try to use the unofficial Google Music API.
EDIT: If you just want to start Google Music use that code:
Code:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address"); //replace the package name
startActivity(LaunchIntent);
nikwen said:
If you select "Always", the default application will be set which you can remove from the settings.
However, there should be a better possibility to do this.
You could try to use the unofficial Google Music API.
EDIT: If you just want to start Google Music use that code:
Code:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address"); //replace the package name
startActivity(LaunchIntent);
Click to expand...
Click to collapse
Actually you can not use this to set a default application. You can try this in the emulator. The selection of Always does not impact the default application setting.
It appears that the Confirmation of "Always" and "Just Once" when ACTION_MAIN is used with a CATEGORY is a misnormer altogether. A default is only set when an Intent contains a mime type. So if you did not use ACTION_MAIN but say GET_CONTENT with a type of audio/* then you can launch with a default player.
Try it your self in the emulator this never sets a default. Seems everyone says it does but certainly the emulator and a real device support the outcomes that a default is never established through his call at all.
Stll looking for a answer but at least I now know that the above will always prompt you to select an application irrespective of any defaults set.
did you try it on a device?
out of ideas said:
did you try it on a device?
Click to expand...
Click to collapse
you mean my original code. Yes I did and the way it behaves is as described. I also went debugging what the makemainselector actually does under these situations. It actually registers the selected application in the packagemanager against the "Android device". Subsequent calls with the APP_MUSIC Category do not get found for some reason. Its like the code does not look for the application that is registered but asks for the number of applications that support the category and then throws a prompt rather than checking if there is a existing default. So either the makemainselector is the wrong thing to use or this just behaves incorrectly. When looking through the developer apps it does state to use makemainselectoractivity for API>15 as the action for the <15 intent of MUSIC_PLAYER is deprecated. However, an intent using MUSIC_PLAYER works corrctly and kicks off the default where as makemainselectoractivity seems to set the default, and kick off the app but does this every time. So either the documentation is incorrect or the code has a bug. If I get time I'll try and look through the code but at present it does not seem that this actually works to select a default allocated application and use it......you have to put up with being prompted and then selecting an option or double clicking every time.
Code:
Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Min SDK 15
PackageManager pm = this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(intent, 0);
Toast.makeText(this, pm.getApplicationLabel(mInfo.activityInfo.applicationInfo), Toast.LENGTH_LONG).show();
startActivity(intent);

Categories

Resources