Android应用程序之间共享文字和图片(二)

时间:2023-12-17 20:28:56

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>