android 获取手机图片分享源码
16lz
2022-07-25
可获取手机图片实现分享。
布局代码: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); }}
更多相关文章
- 读取android手机流量信息
- Android(安卓)Wifi模块分析(三)
- Android中dispatchDraw分析
- Android四大基本组件介绍与生命周期
- android 获取唯一标识
- Android(安卓)Service AIDL
- Android调用天气预报的WebService简单例子
- android打电话发短信
- android 拨打紧急号码,通话时开启免提功能实现