Android share application data

Sharing Content between Android apps

Sharing is caring, as they say, but sharing on Android means something perhaps slightly different. ‘Sharing’ is really shorthand for sending content such as text, formatted text, files, or images between apps.

So if ‘sharing’ == sending content, it makes slightly more sense that it is implemented using ACTION_SEND (or ACTION_SEND_MULTIPLE) Intents and its dozen extras.

While that approach is perfectly valid, I prefer to use ShareCompat, a set of classes in the v4 Support Library designed to make it easy to build intents for sharing content.

Sharing text

Sharing plain text is, as you might imagine, a good place to start. In fact, there’s not a whole lot to it:

ShareCompat.IntentBuilder uses a fluent API where you can chain together multiple method calls, using only the ones you need. For sharing, one of the most important parts is picking the right mime type — this is how apps filter what type of content they can receive. By using text/plain, we signify that our Intent will only contain plain text. Then, of course, setText() is how we actually add the CharSequence to the Intent to send. And while you can certainly send styled text using setText(), there’s no guarantee that the receiving app will honor that styling, so you should ensure that the text is legible with or without styling.

You’ll note we then use resolveActivity() before calling startActivity(). As mentioned in Protecting Implicit Intents with Runtime Checks, this is critical to prevent an ActivityNotFoundException when there is no Activity available to handle the mime type you have selected. While probably not as much of a concern with text/plain, it may be much more common with other types.

Note: when you use startActivity(shareIntent), that respects any default apps the user has set (i.e., if they’ve previously selected sharing all “text/plain” items to a certain app). If you’d like to instead always show a disambiguation chooser, use the intent generated from IntentBuilder.createChooserIntent() as explained in the ACTION_CHOOSER documentation.

Sharing HTML text

Some apps, most notably email clients, also support formatting with HTML. The changes, compared to plain text, are fairly minor:

The differences here are that we use of setHtmlText() in place of setText() and a mime type of text/html replacing text/plain. Here ShareCompat actually does a little bit extra: setHtmlText() also uses Html.fromHtml() to create a fallback formatted text to pass along to the receiving app if you haven’t previously called setText() yourself.

Given that many of the apps that can receive HTML text are email clients, there’s a number of helper methods to set the subject, to:, cc:, and bcc: email addresses as well — consider adding at least a subject to any share intent for best compatibility with email apps.

Читайте также:  Gacha life mod android

Of course, you’ll still want to call resolveActivity() just as before — nothing changes there.

Receiving text

While the focus so far has been on the sending side, it is helpful to know exactly what is happening on the other side (if not just to build a simple receiving app to install on your emulator for testing purposes). Receiving Activities add an intent filter to the Activity:

The action is obviously the more critical part — without that there’s nothing that would denote this as an ACTION_SEND (the action behind sharing). The mime type, same as with our sending code, is also present here. What isn’t as obvious are the two categories. From the element documentation:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

So CATEGORY_DEFAULT is required for our use case. Then, CATEGORY_BROWSABLE allows web pages to natively share into apps without any extra effort required on the receiving side.

And to actually extract the information from the Intent, the useful ShareCompat.IntentReader can be used:

Similar to IntentBuilder, IntentReader is just a simple wrapper that make it easy to extract information.

Sharing files and images

While sending and receiving text is straightforward enough (create text, include it in Intent), sending files (and particularly images — the most common type by far) has an additional wrinkle: file permissions.

The simplest code you might try might look like

And that almost works — the tricky part is in getting a Uri to the File that other apps can actually read, particularly when it comes to Android 6.0 Marshmallow devices and runtime permissions (which include the now dangerous READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions).

My plea: don’t use Uri.fromFile(). It forces receiving apps to have the READ_EXTERNAL_STORAGE permission, won’t work at all if you are trying to share across users, and prior to KitKat, would require your app to have WRITE_EXTERNAL_STORAGE. And really important share targets, like Gmail, won’t have the READ_EXTERNAL_STORAGE permission — so it’ll just fail.

Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.

Once you have it set up, our code becomes:

Using FileProvider.getUriForFile(), you’ll get a Uri actually suitable for sending to another app — they’ll be able to read it without any storage permissions — instead, you are specifically granting them read permission with FLAG_GRANT_READ_URI_PERMISSION.

Note: we don’t call setType() anywhere when building our ShareCompat (even though in the video I did set it). As explained in the setDataAndType() Javadoc, the type is automatically inferred from the data URI using getContentResolver().getType(uriToImage). Since FileProvider returns the correct mime type automatically, we don’t need to manually specify a mime type at all.

If you’re interested in learning more about avoiding the storage permission, consider watching my Forget the Storage Permission talk or at least go through the slides, which covers this topic in depth at 14:55 (slide 11).

Читайте также:  Навигаторы автомобильные 7 дюймов андроид

Receiving files

Receiving files isn’t too different from text because you’re still going to use ShareCompat.IntentReader. For example, to make a Bitmap out of an incoming file, it would look like:

Of course, you’re free to do whatever you want with the InputStream — watch out for images that are so large you hit an OutOfMemoryException. All of the things you know about loading Bitmaps still apply.

The Support Library is your friend

With both ShareCompat (and its IntentBuilder and IntentReader) and FileProvider in the v4 Support Library, you’ll be able to include sharing text, HTML text, and files in your app with the best practices by default.

Источник

Android SharedPreferences Tutorial and Example

In this Android tutorial we are going to see how to use Android SharedPreferences class to store and retrieve application specific persistent data.

Android SharedPreferences Tutorial

  • Android SharedPreferences allows us to store private primitive application data in the form of key-value pair.
  • Android stores shared preference settings as XML file in shared_prefs folder under DATA/data/[application package] directory. The DATA folder can be obtained by calling Environment.getDataDirectory() (usually it is /data).
  • SharedPreferences is application specific, i.e.) the data is lost when you perform one of the options,
    • once you uninstall the application
    • once you clear application data (through Settings)

How to get SharedPreferences instance?

To use shared preferences, you can use one of the following methods,

Method 1:

Use SharedPreferences getSharedPreferences (String name, int mode). This method gets shared preferences from a specified file.

  • PREFS_NAME is the name of the file.
  • Context.MODE_PRIVATE is the operating mode.

Other modes are,

Operating Mode Constant value Description
MODE_PRIVATE 0 File creation mode: the default mode, where the created file can only be accessed by the calling application.
MODE_WORLD_READABLE 1 This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications.
MODE_WORLD_WRITEABLE 2 This constant was deprecated in API level 17.
Creating world-writable files is very dangerous, and likely to cause security holes in applications.
MODE_MULTI_PROCESS 4 This method will check for modification of preferences even if the sharedpreference instance has already been loaded
MODE_APPEND 32768 This will append the new preferences with the already exisiting preferences
MODE_ENABLE_WRITE_AHEAD_LOGGING 8 Database open flag. When it is set , it would enable write ahead logging by default

Method 2:

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context. Here the file is stored as

Store Data in SharedPreferences

To save a value in SharedPreferences, you can use SharedPreferences.Editor class.
Steps:

  1. Get SharedPreferences instance using one of the methods explained above.
  2. Get SharedPreferences.Editor instance by calling edit() method in SharedPreferences instance.
  3. Store values by calling one of the putXXXX() methods.
  4. Commit the editor object.
Читайте также:  Как подключить андроид авто cx5

Only primitive data types can be stored in SharedPreferences. Other methods are,

Retrieve Data from SharedPreferences

To get a value from shared preferences, you can use the SharedPreferences class’ getXXXX methods without the Editor object.
Steps:

  • Get SharedPreferences instance using one of the methods explained above.
  • Call one of the getXXXX() methods using SharedPreferences instance.

Clear SharedPreferences Data

To remove all values from preferences use editor.clear() method as shown below.

To remove a specific key-value pair use editor.remove(KEY) method as shown below.

Android SharedPreferences Example

Project Description

In this Android Example, we will see how to use SharedPreferences to share data from one activity to another.

  • We create a separate SharedPreference utility class with methods to save, get, clear, remove from SharedPreferences.
  • We create two activities,
    • MainActivity – to save the value entered in EditText.
    • SecondActivity – to get the value from SharedPreferences and display it in TextView.

Android Project

Create a new Android project and name it as SharedPreferenceDemo.

Resources

strings.xml

Open res/values/strings.xml and edit to have the content as shown below.

Layout files
activity_main.xml

This XML layout file (activity_main.xml) defines an EditText and a Button which is ued by MainActivity.java. Open activity_main.xml file in res/layout and copy the following content.

activity_second.xml

This XML layout file (activity_second.xml) defines a TextView which is used by SecondActivity.java. Create a new activity_second.xml file in res/layout and copy the following content.

Source files

SharedPreference class

Create a new class SharedPreference in the package com.androidopentutorials.sharedpreference.utils. This class defines methods to save, get and remove shared preferences values.

MainActivity class

Open MainActivity.java class and copy the following code. This class gets value from EditText, stores it in SharedPreferences and starts the second activity.

SecondActivity class

Create a new SecondActivity.java class and copy the following code. This class gets the value from SharedPreferences and displays it in TextView.

Output

MainActivity

When we go directly to second activity, no value is displayed as we have not yet stored the value in shared preference. Once the submit button is pressed, the value is saved in shared preference. Now this value will be shown on subsequent app launches by directly going to the second activity.

SecondActivity

Storing Java objects in SharedPreferences

Android SharedPreferences allows you to store only primitive values or set of strings (java.util.Set ). To store custom Java objects (object, arrays or lists) in SharedPreferences you can use one of the following methods,

  1. Generate toString() method in your class and save the object as String by calling toString()
    • This method is not useful when you want to retrieve the value and construct the object from String.
  2. Using external library such as GSon or Jackson to convert Java object to/from JSON (JavaScript Object Notation). After converting to JSON object, you can store it as string in SharedPreferences. Gson has methods,
    • toJson() – Convert Java object to JSON format
    • fromJson() – Convert JSON into Java object

Источник

Оцените статью