0

Can I edit .txt file opening app programmatically?

I wanna develop a app manager, and implements function like this: edit different type(with different suffix) opening app, or clear default. Is this possible in Android?

tshepang
  • 12,111
  • 21
  • 91
  • 136
thecr0w
  • 2,148
  • 4
  • 33
  • 59
  • 1
    yes you could develop these in android – XepterX Sep 04 '12 at 08:31
  • Does this answer your question? [How can I read a text file in Android?](https://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android) –  Aug 19 '20 at 11:51

1 Answers1

3

You can use:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(<The URI of the data you want to open>);
intent.setType(<MIME type>); // Optional        
startActivity(intent);

This will show you the appliation choser dialog with the applications that can open the data based on the URI scheme/path and/or the MIME type.

You can also use Intent.ACTION_EDIT instead of Intent.ACTION_VIEW if you want to edit the data.

EDIT: Nothing much to show, to open a TXT file you can use:

File file = new File(Environment.getExternalStorageDirectory(), "test.txt");
Uri uri = Uri.parse("file://" + file.getAbsolutePath());

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);        
startActivity(intent);

If there is any application associated with TXT files, the Android OS will prompt you to chose an app, if there is only 1 application capable of opening the specified file type, it will open it using that.

Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • Thanks for quick reply. Can u show me a demo if I have the honor? – thecr0w Sep 04 '12 at 10:33
  • Thank you, you are so kind. But I think I didn't explain meself clear, I don't want to open a specific file after edited. Just change opening app, Then do nothing. Hope you got what I say :) – thecr0w Sep 04 '12 at 11:23
  • You can't change that. The OS decides which app to open based on the intent filters in the manifest files of the installed apps. If there is more than one application that is registered to the specific file type, then the app chooser window will show. You have no way to alter that behaviour. – Adam Monos Sep 04 '12 at 12:38