Sharing text and images between Android apps (1)

Source: Internet
Author: User

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 Description: * Transfer text and images between non-system Android apps * that is, transfer text and images between two self-written apps ** note: * Deploy TestReceiveShare1 project * Deploy TestShare1 project * Reference: * http://blog.csdn.net/xiaanming/article/details/942 8613 */public class MainActivity extends Activity {private TextView mTextView; private ImageView mFirstImageView; private ImageView mSecondImageView; @ Overrideprotected 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); // scenario 1: the content to be shared is text 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) ;}// Scenario 2: the content to be shared is an image 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) ;}// Case 3: the content to be shared is multiple images 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. testreceiv Eshare1.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> <! -- Process the IntentFilter of an image --> <intent-filter> <action android: name = "android. intent. action. SEND "/> <category android: name =" android. intent. category. DEFAULT "/> <data android: mimeType =" image/* "/> </intent-filter> <! -- Process IntentFilter of multiple images --> <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 Description: * Sharing text and pictures between Android apps ** reference: * http://www.vmeitime.com/post/2012-06-08/40027373105 */public class MainActivity extends Activity {private Button mTextButton; private Button mPictureButton; private Button mPicturesButton; @ Overrideprotected 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 {@ Overridepublic void onClick (View view) {switch (view. getId () {case R. id. shareTextButton: shareText ("this is the content to be shared"); break; case R. id. sharePicButton: sharePicture (); break; case R. id. sharePicsButton: sharePictures (); break; default: break ;}}// shared text private void shareText (String string) {Intent intent Intent = new Intent (Intent. ACTION_SEND); intent. putExtra (Intent. EXTRA_TEXT, string); intent. setType ("text/plain"); Intent. createChooser (intent, "shared text"); startActivity (intent) ;}// share an image 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, "share an image"); startActivity (intent);} // share multiple images 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); // This is not a good method: // some models report an error // Uri pictureUri1 = Uri. parse ("file: // mnt/sdcard/test1.png"); // Uri pictureUri2 = Uri. parse ("file: // mnt/sdcard/test2.png"); // incorrect method: // 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, "Sharing multiple images"); startActivity (intent );}}
<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 = "shared 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 = "share an image"/> <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 = "Sharing multiple images"/> </RelativeLayout>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.