Android應用程式之間共用文字和圖片(一)

來源:互聯網
上載者: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描述: * 非系統內建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;@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);//情況一:欲分享的內容是文字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;@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("這是要分享的內容");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="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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.