Android分享



Android分享

调用分享功能

1、分享文本

分享功能使用的隐式启动Activity的方法,这里的Action使用的是 ACTION_SEND 

  1. Intent sendIntent = new Intent();  
  2. sendIntent.setAction(Intent.ACTION_SEND);  
  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  
  4. sendIntent.setType("text/plain");  
  5. startActivity(sendIntent);  
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

效果如下图的图一。

2、改变分享列表标题

    使用上面的分享方式分享列表标题为“使用一下内容完成操作”,Android中提供了Intent.createChooser() ,这样能一直显示分享选择列表,并且修改了分享列表标题内容。

  1. Intent sendIntent = new Intent();  
  2. sendIntent.setAction(Intent.ACTION_SEND);  
  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  
  4. sendIntent.setType("text/plain");  
  5. startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));  
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

使用Intent.createChooser()的好处:

 If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

                    


    分享功能不只是Intent.EXTRA_TEXT,还可以 EXTRA_EMAILEXTRA_CCEXTRA_BCC,EXTRA_SUBJECT. 只需要接受方完成响应数据接受。


3、分享图片

    分享功能还支持二进制内容(Binary Content),但是多数是处理的图片,因为shareIntent.setType("image/jpeg")这一项设置了内容类型。可也以是其他类型,需要接受方支持。

  1. Intent shareIntent = new Intent();  
  2. shareIntent.setAction(Intent.ACTION_SEND);  
  3. shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);  
  4. shareIntent.setType("image/jpeg");  
  5. startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));  
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

4、分享图片列表

    分享功能不仅支持单张图片,还支持图片列表,这里还是说的范围太窄了,应该声明不仅仅是图片。

  1. ArrayList<Uri> imageUris = new ArrayList<Uri>();  
  2. imageUris.add(imageUri1); // Add your image URIs here  
  3. imageUris.add(imageUri2);  
  4.   
  5. Intent shareIntent = new Intent();  
  6. shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);  
  7. shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);  
  8. shareIntent.setType("image/*");  
  9. startActivity(Intent.createChooser(shareIntent, "Share images to.."));  
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));


实现分享功能

    上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用Activtiy实现的,Activity需要声明 <intent-filter> 。


声明intent-filter

  1. <activity  
  2.            android:name="com.example.sharedemo.ShareActivity"  
  3.            android:label="@string/app_name" >  
  4.            <intent-filter>  
  5.                <action android:name="android.intent.action.SEND" />  
  6.   
  7.                <category android:name="android.intent.category.DEFAULT" />  
  8.   
  9.                <data android:mimeType="image/*" />  
  10.            </intent-filter>  
  11.            <intent-filter>  
  12.                <action android:name="android.intent.action.SEND" />  
  13.   
  14.                <category android:name="android.intent.category.DEFAULT" />  
  15.   
  16.                <data android:mimeType="text/plain" />  
  17.            </intent-filter>  
  18.            <intent-filter>  
  19.                <action android:name="android.intent.action.SEND_MULTIPLE" />  
  20.   
  21.                <category android:name="android.intent.category.DEFAULT" />  
  22.   
  23.                <data android:mimeType="image/*" />  
  24.            </intent-filter>  
  25.        </activity>  
 <activity
            android:name="com.example.sharedemo.ShareActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>

上面声明了三种intent-filter,当然可以更多,这里只是举个例子,


处理接收数据

声明了intent-filter,响应的Activity就要处理响应的数据,示例如下:

  1. public class ShareActivity extends Activity{  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.           
  8.         // Get intent, action and MIME type  
  9.         Intent intent = getIntent();  
  10.         String action = intent.getAction();  
  11.         String type = intent.getType();  
  12.   
  13.         if (Intent.ACTION_SEND.equals(action) && type != null) {  
  14.             if ("text/plain".equals(type)) {  
  15.                 handleSendText(intent); // Handle text being sent  
  16.             } else if (type.startsWith("image/")) {  
  17.                 handleSendImage(intent); // Handle single image being sent  
  18.             }  
  19.         } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {  
  20.             if (type.startsWith("image/")) {  
  21.                 handleSendMultipleImages(intent); // Handle multiple images being sent  
  22.             }  
  23.         } else {  
  24.             // Handle other intents, such as being started from the home screen  
  25.         }  
  26.     }  
  27.   
  28.     void handleSendText(Intent intent) {  
  29.         String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);  
  30.         String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);  
  31.         if (sharedText != null) {  
  32.             // Update UI to reflect text being shared  
  33.         }  
  34.     }  
  35.   
  36.     void handleSendImage(Intent intent) {  
  37.         Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);  
  38.         if (imageUri != null) {  
  39.             // Update UI to reflect image being shared  
  40.         }  
  41.     }  
  42.   
  43.     void handleSendMultipleImages(Intent intent) {  
  44.         ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);  
  45.         if (imageUris != null) {  
  46.             // Update UI to reflect multiple images being shared  
  47.         }  
  48.     }  
  49. }  
public class ShareActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// Get intent, action and MIME type
	    Intent intent = getIntent();
	    String action = intent.getAction();
	    String type = intent.getType();

	    if (Intent.ACTION_SEND.equals(action) && type != null) {
	        if ("text/plain".equals(type)) {
	            handleSendText(intent); // Handle text being sent
	        } else if (type.startsWith("image/")) {
	            handleSendImage(intent); // Handle single image being sent
	        }
	    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
	        if (type.startsWith("image/")) {
	            handleSendMultipleImages(intent); // Handle multiple images being sent
	        }
	    } else {
	        // Handle other intents, such as being started from the home screen
	    }
	}

	void handleSendText(Intent intent) {
	    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
	    String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
	    if (sharedText != null) {
	        // Update UI to reflect text being shared
	    }
	}

	void handleSendImage(Intent intent) {
	    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
	    if (imageUri != null) {
	        // Update UI to reflect image being shared
	    }
	}

	void handleSendMultipleImages(Intent intent) {
	    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
	    if (imageUris != null) {
	        // Update UI to reflect multiple images being shared
	    }
	}
}

通过声明intent-filter,处理接受到的数据就能完成分享的接收功能。

原文连接:http://blog.csdn.net/xyz_lmn/article/details/16856843



Android 系统中,可以通过 Intent 来实现文件的分享功能。示例代码如下: ```java // 指定要分享的文件路径 String filePath = "/sdcard/test.txt"; // 创建一个 Intent Intent intent = new Intent(Intent.ACTION_SEND); // 设置 Intent 的类型,表示要分享文件 intent.setType("*/*"); // 添加要分享的文件 Uri File file = new File(filePath); Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); intent.putExtra(Intent.EXTRA_STREAM, uri); // 启动分享界面 startActivity(Intent.createChooser(intent, "分享文件")); ``` 其中,`FileProvider.getUriForFile()` 方法用于获取文件的 Uri,需要在 AndroidManifest.xml 文件中注册 fileprovider,示例如下: ```xml <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 在 res/xml 目录下,创建一个名为 file_paths.xml 的文件,用于指定要分享的文件路径,示例如下: ```xml <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="." /> </paths> ``` 上述代码中,`<external-path>` 标签表示分享的文件路径为外部存储器的根目录。如果要分享其他目录的文件,可以添加相应的 `<external-path>` 或 `<files-path>` 标签。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值