49046

Android Intents - Difference between SetDataAndType and just setData and setType?

So I am writing an intent that should launch the gallery to view an image passed into the intent with it's Uri. This code works fine, does exactly what I want it to do:

private Intent makeGalleryIntent(String pathToImageFile) { Intent mGalleryIntent = new Intent(Intent.ACTION_VIEW); mGalleryIntent.setDataAndType(Uri.parse("file://" + pathToImageFile), "image/*"); return mGalleryIntent; }

But when I tried to play with it (just trying different things out as I learn), this code either crashes on the emulator because it can't launch the camera, or just pulls up my gallery on my physical device:

private Intent makeGalleryIntent(String pathToImageFile) { Intent mGalleryIntent = new Intent(Intent.ACTION_VIEW); mGalleryIntent.setData(Uri.parse("file://" + pathToImageFile)); mGalleryIntent.setType("image/*"); return mGalleryIntent; }

They both look like they should do the exact same thing. Also, is there a way I could have set all of that properly just using the Intent constructor?

Answer1:

From the documentation: Intent Documentation

To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().

Caution: 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.

Ive never really gotten an answer as to WHY they would nullify each other..If you have an idea as to why id love to hear it!

Recommend

  • Android load local html file and display in web browser
  • capture image from camera and crop not working
  • Getting permission denied when using a URI despite FLAG_GRANT_READ_URI_PERMISSION being set
  • how to change install an update dialog message text
  • How can I write a program to install and uninstall an app over android device?
  • Open pdf file on any android device
  • save the image to the gallery in android
  • How to Crop the image in android marshmallow by using default crop intent?
  • How to download & install .apk file in Android application?
  • When trying to open a file with Intent getting 'Media not found' error
  • How to get a callback from google maps in Android application, when navigation is complete
  • How to enqueue music to default android music player via app?
  • Android - Call default browser with and redirect to a designated url
  • Read text file that is not in the main package in a runnable jar
  • IE7 and TinyMCE with Plone
  • Android Google Maps API v2 start navigation
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Conversion from string “a” to type 'Boolean' is not valid
  • how to upload multiple files in c# windows application
  • Display java JPanel in a JFrame
  • Appending Character to Character Array In C
  • Unable to decode certificate at client new X509Certificate2()
  • OOP Javascript - Is “get property” method necessary?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Passing 1 of 3 values to a Sub
  • How to test if a URL from an Eclipse bundle is a directory?
  • ActiveRecord query for a count of new users by day
  • Set the selected item in dropdownlist in MVC3
  • How to create a file in java without a extension
  • Spring Data JPA custom method causing PropertyReferenceException
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++