Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why setDataAndType() for an android intent works fine when setData() and setType() are not working?

Tags:

android

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.

  1. 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"

  2. 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

  1. 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

  2. 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?

like image 864
Siva Kranthi Kumar Avatar asked Dec 05 '12 08:12

Siva Kranthi Kumar


2 Answers

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)).
...

like image 82
ZehnVon12 Avatar answered Oct 03 '22 12:10

ZehnVon12


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.

like image 32
Nithin Raja Avatar answered Oct 03 '22 11:10

Nithin Raja