• Aucun résultat trouvé

Set the Share Intent

Dans le document 2. Building Your First App (Page 114-117)

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item

android:id="@+id/menu_item_share"

android:showAsAction="ifRoom"

android:title="Share"

android:actionProviderClass=

"android.widget.ShareActionProvider" />

...

</menu>

This delegates responsibility for the item's appearance and function to ShareActionProvider.

However, you will need to tell the provider what you would like to share.

Set the Share Intent

In order for ShareActionProvider to function, you must provide it a share intent. This share intent should be the same as described in the Sending Simple Data to Other Apps lesson, with action ACTION_SEND and additional data set via extras like EXTRA_TEXT and EXTRA_STREAM. To assign a

This lesson teaches you to

• Update Menu Declarations

• Set the Share Intent You should also read

• Action Bar

Adding an Easy Share Action

115

Content from developer.android.com/training/sharing/shareaction.html through their Creative Commons Attribution 2.5 license

share intent, first find the corresponding MenuItem while inflating your menu resource in your Activity or Fragment. Next, call MenuItem.getActionProvider() to retrieve an instance of

ShareActionProvider. Use setShareIntent() to update the share intent associated with that action item. Here's an example:

private ShareActionProvider mShareActionProvider;

...

@Override

public boolean onCreateOptionsMenu(Menu menu) { // Inflate menu resource file.

getMenuInflater().inflate(R.menu.share_menu, menu);

// Locate MenuItem with ShareActionProvider

MenuItem item = menu.findItem(R.id.menu_item_share);

// Fetch and store ShareActionProvider

mShareActionProvider = (ShareActionProvider) item.getActionProvider();

// Return true to display menu return true;

}

// Call to update the share intent

private void setShareIntent(Intent shareIntent) { if (mShareActionProvider != null) {

mShareActionProvider.setShareIntent(shareIntent);

} }

You may only need to set the share intent once during the creation of your menus, or you may want to set it and then update it as the UI changes. For example, when you view photos full screen in the Gallery app, the sharing intent changes as you flip between photos.

For further discussion about the ShareActionProvider object, see the Action Bar guide.

Sharing Files

116

Content from developer.android.com/training/secure-file-sharing/index.html through their Creative Commons Attribution 2.5 license

38. Sharing Files

Content from developer.android.com/training/secure-file-sharing/index.html through their Creative Commons Attribution 2.5 license

Apps often have a need to offer one or more of their files to another app. For example, an image gallery may want to offer files to image editors, or a file management app may want to allow users to copy and paste files between areas in external storage. One way a sending app can share a file is to respond to a request from the receiving app.

In all cases, the only secure way to offer a file from your app to another app is to send the receiving app the file's content URI and grant temporary access permissions to that URI.

Content URIs with temporary URI access permissions are secure because they apply only to the app that receives the URI, and they expire automatically. The Android FileProvider

component provides the method getUriForFile() for generating a file's content URI.

If you want to share small amounts of text or numeric data between apps, you should send an Intent that contains the data. To learn how to send simple data with an Intent, see the training class Sharing Simple Data.

This class explains how to securely share files from your app to another app using content URIs generated by the Android FileProvider component and temporary permissions that you grant to the receiving app for the content URI.

Lessons

Setting Up File Sharing

Learn how to set up your app to share files.

Sharing a File

Learn how to offer a file to another app by generating a content URI for the file, granting access permissions to the URI, and sending the URI to the app.

Requesting a Shared File

Learn how to request a file shared by another app, receive the content URI for the file, and use the content URI to open the file.

Retrieving File Information

Learn how an app can use a content URI generated by a FileProvider to retrieve file information including MIME type and file size.

Dependencies and prerequisites

• Android 1.6 (API Level 4) or higher

• Familiarity with file operations such as opening, reading, and writing files You should also read

• Storage Options

• Saving Files

• Sharing Simple Data

Setting Up File Sharing

117

Content from developer.android.com/training/secure-file-sharing/setup-sharing.html through their Creative Commons Attribution 2.5 license

39. Setting Up File Sharing

Content from developer.android.com/training/secure-file-sharing/setup-sharing.html through their Creative Commons Attribution 2.5 license

To securely offer a file from your app to another app, you need to configure your app to offer a secure handle to the file, in the form of a content URI. The Android FileProvider component generates content URIs for files, based on specifications you provide in XML. This lesson shows you how to add the default implementation of FileProvider to your app, and how to specify the files you want to offer to other apps.

Note: The FileProvider class is part of the v4 Support Library. For information about including

this library in your application, see Support Library Setup.

Dans le document 2. Building Your First App (Page 114-117)