Difference between Intent.ACTION_GET_CONTENT and Intent.ACTION_PICK - java

I'm trying to let the user choose any image that they want on their device to use as a wallpaper in this wallpaper application I'm building. For some reason when I write:
Intent myIntent = new Intent(Intent.ACTION_PICK);
myIntent.setType("image/*");
startActivityForResult(myIntent, 100);
I go straight into the gallery, but when I write:
Intent myIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
myIntent.setType("image/*");
startActivityForResult(myIntent, 100);
I get to choose from Gallery, or Google Drive. What is the best way to let the user choose what app to retrieve the picture from every time? Or why do those two different intent constants make a difference?

Your first Intent is invalid. The protocol for ACTION_PICK requires you to supply a Uri indicating the collection you are picking from.
What is the best way to let the user choose what app to retrieve the picture from every time?
If you want the user to choose something based on MIME type, use ACTION_GET_CONTENT.
If you have some specific collection (identified by a Uri) that you want the user to pick from, use ACTION_PICK.
In case of a tie, go with ACTION_GET_CONTENT. While ACTION_PICK is not formally deprecated, Dianne Hackborn recommends ACTION_GET_CONTENT.

The modern action is ACTION_GET_CONTENT, which is much better supported,
ACTION_PICK :
Activity Action: Pick an item from the data, returning what was selected.
Input: getData() is URI containing a directory of data (vnd.android.cursor.dir/*) from which to pick an item.
Output: The URI of the item that was picked.
Constant Value: "android.intent.action.PICK"
Difference :-
Activity Action: Allow the user to select a particular kind of data and return it.
This is different than ACTION_PICK in that here we just say what kind of data is desired, not a URI of existing data from which the user can pick.
A ACTION_GET_CONTENT could allow the user to create the data as it runs (for example taking a picture or recording a sound), let them browse over the web and download the desired data, etc.
Reference http://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT

public static final String ACTION_GET_CONTENT
Added in API level 1
Activity Action: Allow the user to select a
particular kind of data and return it. This is different than
ACTION_PICK in that here we just say what kind of data is desired, not
a URI of existing data from which the user can pick. A
ACTION_GET_CONTENT could allow the user to create the data as it runs
(for example taking a picture or recording a sound), let them browse
over the web and download the desired data, etc.
via http://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT

Related

Android: Storage Options

I am creating an application that allows the user to store information about food recipes that they can share on social media. I have an activity that allows the user to upload an image, write ingredients and directions for the recipe. In the past I have worked with shared preferences for saving user information, however, I know that the information stored is unordered. So I want to know what type of storage I should use to achieve this outcome....
My activity that saves user data:
From this I want to load the information into a previous activities list view that will have this type of list element layout:
From this what type of storage approach should I take? if I use shared preferences can I just place the parts I need into the elements of the list manually, for example, extracting the image saved from the user and placing that inside the image section of the listviews? or will the limitations of shared get in the way and maybe use internal storage? what would be the best approach?
Form a structured JSON object out of your data and you can use REALM. It is a new and easy alternative to SQLite.
It is very fast, easy to learn and integrate in your app.
Also, it is scalable means if you decide that you will be taking the same data to your backend then it will be easy for you to handle large amount of data.

capture user asked data from echo

I'm writing a code for Amazon Alexa. And in the developer.amazon.com, we configure the intents, utterances etc... Here I need to get the question asked by the user.
I'm sure that this works using the intents but I will need the utterances. For eg:
GetTheData get me the user data
GetTheData get me user details
Here I'm able to get the GetTheData printed using intent.getName(), But i want to know if there is a way to get the content after the intent name i.e. get me the user data. I want to know this for my research purpose.
Please let me know how can I get this.
Thanks
No, unfortunately there is no way to get this information with the ASK/echo.
Your only solutions at this time would be to split that into one intent per utterance, or switch to google home (its SDK can give you the raw user text).

Android, Java, Choose image from gallery. Store link and open later

I want to select one or more images in application (Activity) and use them in Service. I receive Uris using Intent like this:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "Select Picture"), RESULT_LOAD_IMG);
Uris I get work but only for some time. From what I understand Uris are temporary. In my case I need permanent links to image files.
Is there a simple way to receive usable (in this situation) link to an image file?
Is there a simple way to receive usable (in this situation) link to an image file?
Copy the image to your app's internal storage.
On Android 4.4+, you could use ACTION_OPEN_DOCUMENT and the Storage Access Framework, instead of ACTION_GET_CONTENT (or, worse, ACTION_PICK). With the Storage Access Framework, you can request to take persistent permissions for the Uri, which increases the odds that you will be able to access the content in the future. However, even in that case, the user can always get rid of the image in question, eliminating your access to it.

How to keep track of information regarding fragments in Android

I have a fragment which is sort of like a test. The user can choose from a spinners menu if this test has passed, failed or undecided. Once the user leaves this fragment and then comes back to it later, I want the spinners menu to display what the user had previously selected.
Can anyone tell me how to keep track of this information? I am a beginner with Java and Android programming. Also, let me know if further explanation is required.
It probably depends on what scope you want to be able to refer to these tests in. For example, do you want these fragments to be emptied after the user closes the app, or not?
If it is just while in the app "session", you could just create an object in your fragment and override the onResume method to populate the spinners with the appropriate data.
If you want to save the info after the app is closed, there are a couple of options. For smaller sets of data you could use shared preferences
if you have an undetermined amount of "tests", I would advise saving whatever data you need to a file, and parsing it to fill the fields in the fragments
Take a look at this to get an idea of how to save data: http://developer.android.com/intl/es/training/basics/data-storage/files.html
I've done something similar to what your doing. I used shared preferences to save the string like so
Saving the string you got from the spinner in a fragment
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
mSharedPreferencesEditor.putString("my_unique_key", myString);
// commit changes
mSharedPreferencesEditor.apply();
Remember to always apply the changes after you made them and you can save as many strings as you want as long as they each have a unique key.
Then to retrieve the value just
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String myStringValue = mSharedPreferences.getString("my_unique_key", null);
The second argument (null) will be returned if it could not find any data that had that unique key. (i.e if you haven't saved it yet)
You can also use another string instead of null like this
String myStringValue = mSharedPreferences.getString("my_unique_key", "could not find value");
or if you want to bundle a bunch of data together like say create an object to save a bunch of strings then save that object to a file I could show you that if you wanted to

How should i save my app data in my android app?

I am making my first android app. I want to save an array of strings in one activity and then in second activity i want to show those strings as a list. I am using android platform 2.2 with API 8 So i can not use putStringSet(). Is there any way to save my data as a text file in my app? Then may be i can just add a new line in that file whenever user adds a new string. And while making list view i can parse it on basis of new line and make string array.
Thanks for your help.
Use File Handling.
Easy to use for beginners and efficient for your purpose.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
There is a method putStringArrayListExtra("id", ArrayList list) in Intent to use before starting it. Then in the launched Activity from the Intent, use getStringArrayList("id").
For example:
Intent intent = new Intent(MainActivity.this.getApplicationContext(), NewActivity.class);
intent.putStringArrayListExtra("id", yourArrayList)
MainActivity.this.startActivity(intent);
Then on NewActivity onCreate() method
ArrayList<String> list = getIntent().getExtras().getStringArrayList("id");
But you can use putStringArray, or putStringArrayList wrapped into a Bundle.
Bundle are passed via Intents.
-> http://developer.android.com/reference/android/os/Bundle.html#putStringArray(java.lang.String, java.lang.String[])
Then, if you want to save it as a file, then you have to use this method : http://www.java-forums.org/advanced-java/13852-saving-arraylist-file.html
But i think you prefer pass the String list directly.
Why are you using files for that?
Just check sharedPreferences and sqliteDb
It may work for you..shared preferences are easy to handle

Categories