Android应用程序之间共享文字和图片(一)
以下为TestReceiveShare1工程
MainActivity如下:
package cn.testreceiveshare1;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
/**
* Demo描述:
* 非系统自带Android应用之间传递文字和图片
* 即在两个自写的应用之间传递文字和图片
*
* 注意事项:
* 先部署TestReceiveShare1工程
* 再部署TestShare1工程
*
* 参考资料:
* http://blog.csdn.net/xiaanming/article/details/9428613
*/
public class MainActivity extends Activity {
private TextView mTextView;
private ImageView mFirstImageView;
private ImageView mSecondImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
handleReceivedIntent();
} private void initViews(){
mTextView=(TextView) findViewById(R.id.textView);
mFirstImageView=(ImageView) findViewById(R.id.firstImageView);
mSecondImageView=(ImageView) findViewById(R.id.secondImageView);
} private void handleReceivedIntent(){
Intent intent=this.getIntent();
String aciton=intent.getAction();
String type=intent.getType();
System.out.println("aciton="+aciton+",type="+type); //情况一:欲分享的内容是文字
if (aciton!=null&&type!=null&&
Intent.ACTION_SEND.equals(aciton)&&"text/plain".equals(type)) {
String content=intent.getStringExtra(Intent.EXTRA_TEXT);
System.out.println("content="+content);
mTextView.setText(content);
} //情况二:欲分享的内容是一张图片
if (aciton!=null&&type!=null&&
Intent.ACTION_SEND.equals(aciton)&&"image/jpeg".equals(type)) {
Uri pictureUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (pictureUri != null) {
System.out.println("pictureUri="+pictureUri);
mFirstImageView.setImageURI(pictureUri);
}
} //情况三:欲分享的内容是多张图片
if (aciton!=null&&type!=null&&
Intent.ACTION_SEND_MULTIPLE.equals(aciton)&&"image/jpeg".equals(type)) {
ArrayList<Uri> pictureUrisArrayList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (pictureUrisArrayList.size()>0) {
System.out.println("pictureUrisArrayList.size()="+pictureUrisArrayList.size());
mFirstImageView.setImageURI(pictureUrisArrayList.get(0));
mSecondImageView.setImageURI(pictureUrisArrayList.get(1));
}
} }
}
main.xml如下:
<RelativeLayout 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"
> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:layout_marginTop="50dip"
/> <ImageView
android:id="@+id/firstImageView"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dip"
/> <ImageView
android:id="@+id/secondImageView"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="290dip"
/> </RelativeLayout>
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.testreceiveshare1"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.testreceiveshare1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> <!-- 处理文字的IntentFilter-->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter> <!-- 处理一张图片的IntentFilter -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter> <!-- 处理多张图片的IntentFilter -->
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter> </activity>
</application> </manifest>
以下为TestShare1工程
MainActivity如下:
package cn.testshare1;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Demo描述:
* Android应用程序之间共享文字和图片
*
* 参考资料:
* http://www.vmeitime.com/post/2012-06-08/40027373105
*/
public class MainActivity extends Activity {
private Button mTextButton;
private Button mPictureButton;
private Button mPicturesButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mTextButton=(Button) findViewById(R.id.shareTextButton);
mTextButton.setOnClickListener(new ClickListenerImpl());
mPictureButton=(Button) findViewById(R.id.sharePicButton);
mPictureButton.setOnClickListener(new ClickListenerImpl());
mPicturesButton=(Button) findViewById(R.id.sharePicsButton);
mPicturesButton.setOnClickListener(new ClickListenerImpl());
} private class ClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.shareTextButton:
shareText("这是要分享的内容");
break;
case R.id.sharePicButton:
sharePicture();
break;
case R.id.sharePicsButton:
sharePictures();
break; default:
break;
} } } //共享文字
private void shareText(String string) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, string);
intent.setType("text/plain");
Intent.createChooser(intent,"共享文字");
startActivity(intent);
} //共享一张图片
private void sharePicture(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"test1.png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/jpeg");
Intent.createChooser(intent, "共享一张图片");
startActivity(intent);
} //共享多张图片
private void sharePictures(){
ArrayList<Uri> picturesUriArrayList=new ArrayList<Uri>(); File pictureFile1=new File
(Environment.getExternalStorageDirectory()+File.separator+"test1.png");
File pictureFile2=new File
(Environment.getExternalStorageDirectory()+File.separator+"test2.png"); Uri pictureUri1=Uri.fromFile(pictureFile1);
Uri pictureUri2=Uri.fromFile(pictureFile2); //不是很好的方式:
//因为某些机型报错
//Uri pictureUri1=Uri.parse("file:///mnt/sdcard/test1.png");
//Uri pictureUri2=Uri.parse("file:///mnt/sdcard/test2.png"); //错误的方式:
//Uri pictureUri1=Uri.parse
//(Environment.getExternalStorageDirectory()+File.separator+"test1.png");
//Uri pictureUri2=Uri.parse
//(Environment.getExternalStorageDirectory()+File.separator+"test2.png"); picturesUriArrayList.add(pictureUri1);
picturesUriArrayList.add(pictureUri2); Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, picturesUriArrayList);
intent.setType("image/jpeg");
Intent.createChooser(intent, "共享多张图片");
startActivity(intent);
} }
main.xml如下:
<RelativeLayout 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"
> <Button
android:id="@+id/shareTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:text="共享文字"
/>
<Button
android:id="@+id/sharePicButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dip"
android:text="共享一张图片"
/>
<Button
android:id="@+id/sharePicsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="250dip"
android:text="共享多张图片"
/> </RelativeLayout>
Android应用程序之间共享文字和图片(一)的更多相关文章
- Android应用程序之间共享文字和图片(二)
MainActivity如下: package cn.testshare1; import java.io.File; import java.util.ArrayList; import andro ...
- Android应用程序组件Content Provider在应用程序之间共享数据的原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6967204 在Android系统中,不同的应用 ...
- Android 应用程序之间内容分享详解(二)
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9428613 Android 应用程序之间内容分享详解(一) 之前给大家分享了你开发的应 ...
- 程序与程序之间共享对象:MarshalByRefObject
源自于:http://stackoverflow.com/questions/439173/message-pumps-and-appdomains/442316 程序与程序之间共享对象: Marsh ...
- DELPHI编写服务程序总结(在系统服务和桌面程序之间共享内存,在服务中使用COM组件)
DELPHI编写服务程序总结 一.服务程序和桌面程序的区别 Windows 2000/XP/2003等支持一种叫做“系统服务程序”的进程,系统服务和桌面程序的区别是:系统服务不用登陆系统即可运行:系统 ...
- Android 应用程序之间内容分享详解(一)
一个Andoird应用程序的重要的地方是他们有相互沟通和整合的能力,一个应用程序可以和另一个应用程序交互,接下来我们来看看Android应用之间的内容分享 当你构建Intent的时候,必须要指定Int ...
- Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7867340 在前面一篇文章中,我们分析了And ...
- Android应用程序组件Content Provider的共享数据更新通知机制分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6985171 在Android系统中,应用程序组 ...
- iOS应用程序间共享数据
我们知道iOS由于沙盒的存在,应用程序不能越过自己的区域去访问别的存储空间的内容,不过可能有许多场景我们需要在应用程序之间共享数据,比如多个应用共用用户名密码进行登录等.虽然我们不能直接通过文件系统来 ...
随机推荐
- 015_xml_函数
015_xml_函数 --环境准备******************************************************************* USE test --f:/t ...
- 创建dblink遇到一系列问题
创建dblink遇到一系列问题,有时间 把问题整理一下
- [转]Delphi : keydown与keypress的区别,组合键
Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...
- hibernate_validator_07
一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...
- Java中的继承与组合
本文主要说明Java中继承与组合的概念,以及它们之间的联系与区别.首先文章会给出一小段代码示例,用于展示到底什么是继承.然后演示如何通过“组合”来改进这种继承的设计机制.最后总结这两者的应用场景,即到 ...
- Python django admin 替换表单控件
测试版本: Python 2.7 Django 1.6.2 models.py from django.db import models class Article(models.Model): ti ...
- PAT - IO-01. 表格输出(5)
题目: 本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ----------------------------------- ...
- mysql主从复制 (超简单) 转载
怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3.修改root的密码 ...
- ie6789和其他浏览器之间的鼠标左、中、右键的event.button不一致的办法
左键 中键 右键 Ie6 1 4 2 Ie7 1 4 2 Ie8 1 4 2 Ie9和其它 0 1 2 以下代码将IE6/7/8的值转换成符合W3C标准的方法: var ie678 = !-[1, ...
- [Struts2学习笔记] -- 简单的类型转换
接下来学习一下Struts2简单的类型转换,Struts2基于ognl.jar实现了简单类型的数据转换.比如jsp页面中的form值与字段值的转换,下面写一个例子. 1.创建一个jsp页面,编写一个f ...