I had one issue with file editing in android using implicit intents, it got solved know, It took lot of time and permutations and combinations to solve it, but still finally I am left with doubt, The problem got solved but my quest to know why the problem got solved is not solved.
Please let me know if in case u have any clue on this.
Coming to my problem.
I have an activity. I have a Button in the activity. I want to open a pre existing log file (which is a text file example log.txt) stored in the location "/mnt/sdcard/xxx/log.txt"
The below is the implicit intent code i wrote and i ended up with an exception " No activity found"
code1: which i tried and got exception
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT,uri);
viewTestLogFileIntent.setType("text/plain");
code2: which i tried and got exception
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT);
viewTestLogFileIntent.setData(uri);
viewTestLogFileIntent.setType("text/plain");
code3: which i tried and working fine
Uri uri = Uri.parse("file:///sdcard/xxx/log.txt");
Intent viewTestLogFileIntent = new Intent(Intent.ACTION_EDIT);
viewTestLogFileIntent.setDataAndType(uri,"text/plain");
Two doubts i have are
First of all my file is located in /mnt/sdcard/xxx/log.txt this i can clearly see in the file system in DDMS view of eclipse, but how is it working when i give the file link in uri as "file:///sdcard/xxx/log.txt" where i skipped /mnt from path
what is wrong with code1 and code2? what ever is the data and type i am setting in code3 i am setting same data and type on intent but with different methods like setData() and setType() seperately. why are they ( code2 & code1) not working? why is the code3 working?
Please see the Javadoc of the method setType(String type)
in the class Intent
:
...
This method automatically clears any data that was previously set (for example by setData(Uri)).
...
If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With