Android应用程序之间共享文字和图片(一)
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));
}
}
}
}
<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>
<?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>
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补充:移动开发 , Android ,