可获取手机图片实现分享。

布局代码:main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="wind.com.shared.MainActivity">    <Button        android:id="@+id/share"        android:text="分享"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

实现代码:MainActivity.xml

package wind.com.shared;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    Button share;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        initUI();    }    private void initUI() {        share = (Button)findViewById(R.id.share);        share.setOnClickListener(new shareListener());    }    /**     * 调用手机系统分享     */    public class shareListener implements View.OnClickListener{        @Override        public void onClick(View view) {            Intent intent = new Intent();                /* 开启Pictures画面Type设定为image */            intent.setType("image/*");                /* 使用Intent.ACTION_GET_CONTENT这个Action */            intent.setAction(Intent.ACTION_GET_CONTENT);                /* 取得相片后返回本画面 */            startActivityForResult(intent, 1);        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_OK) {            Uri uri = data.getData();          //  ContentResolver cr = this.getContentResolver();         //   try {               // Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));               // ImageView image = (ImageView) findViewById(R.id.image);                /* 将Bitmap设定到ImageView */               // image.setImageBitmap(bitmap);            //调用手机系统分享                Intent intent = new Intent(Intent.ACTION_SEND);                intent.setType("image/*");                intent.putExtra(Intent.EXTRA_STREAM,uri);                intent.putExtra(Intent.EXTRA_SUBJECT, "Share");                intent.putExtra(Intent.EXTRA_TEXT,"xingxingdongmanwang");                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                startActivity(Intent.createChooser(intent, getTitle()));          //  } catch (Exception e) {         //       Log.e("Exception", e.getMessage(),e);         //   }        }        super.onActivityResult(requestCode, resultCode, data);    }}


更多相关文章

  1. 读取android手机流量信息
  2. Android(安卓)Wifi模块分析(三)
  3. Android中dispatchDraw分析
  4. Android四大基本组件介绍与生命周期
  5. android 获取唯一标识
  6. Android(安卓)Service AIDL
  7. Android调用天气预报的WebService简单例子
  8. android打电话发短信
  9. android 拨打紧急号码,通话时开启免提功能实现

随机推荐

  1. [笔记] android/iOS自动化测试神器Appium
  2. 最新Android开发视频教程(共6章)Android(安
  3. React Native封装Android原生UI和Android
  4. Android平台中进程与线程的基本知识
  5. Android:WebView与Javascript交互(相互调用
  6. 在 Android(安卓)上使用协程(三) :Real Work
  7. Android休眠机制
  8. 简析Android对Linux内核的改动
  9. Android系列教程(2):为 TextView组件加上边
  10. android 与 unity 的那些事