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工程
 * 
 * 參考資料:
 * https://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="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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="https://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應用程序之間共享文字和圖片
 * 
 * 參考資料:
 * https://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);
	}

}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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>

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *