ActionBar之接受分享的文本和图片

在之前的两篇博文中(文本分享:点击打开链接;图片分享:点击打开链接)我们分别实现了文本和图片的分享,那么在本篇博文中我们要实现的是

来接受分享的文本和图片并把它们显示在Activity中。

首先我们在布局文件中定义一个TextView和ImageView来显示接受的文本和图片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
接下来是在MainActivity函数中的具体实现:

package com.example.receive;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tv;
	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		iv = (ImageView) findViewById(R.id.iv);
		// 获得传来的Intent对象
		Intent intent = getIntent();
		// 获得传来的意图对象中的动作
		String action = intent.getAction();
		// 获得intent的类型
		String type = intent.getType();
		// 调用方法
		getDataIntent(intent, action, type);
	}

	private void getDataIntent(Intent intent, String action, String type) {
		// 判断意图对象的动作是否为Intent.ACTION_SEND,就是我们在文本分享和图片分享中设置的发送动作
		if (Intent.ACTION_SEND.equals(action)) {
			// 判断intent的type类型,就是我们发送的数据的类型:文本或者图片
			if ("text/*".equals(type)) {
				// 得到分享过来的文本信息:Intent.EXTRA_TEXT
				String value = intent.getStringExtra(Intent.EXTRA_TEXT);
				tv.setText(value);
			} else if ("image/*".equals(type)) {
				// 得到分享过来的图片的流信息:Intent.EXTRA_STREAM,这是我们分享的图片的路径
				String value = intent.getStringExtra(Intent.EXTRA_STREAM);
				// 通过路径设置位图
				Bitmap bitmap = BitmapFactory.decodeFile(value);
				iv.setImageBitmap(bitmap);
			}
		}
	}

}
然后是在AndroidManifest清单文件中的配置:

①读取文件的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
②在activity条目下另外配置两个<intent-filter>:

<activity
            android:name="com.example.receive.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <!-- 需要添加的两个配置 -->
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

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

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

                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
运行结果:

分享并接受显示文本:第一个图片中的右上角的机器人图标就是”_07接受信息“工程,我们通过点击来进行数据的分享



分享并显示图片:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值