获取手机图片裁剪后分享。
代码下载地址:http://download.csdn.net/detail/u011324501/9530791
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局代码: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" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>代码实现:MainActivity.java
package wind.com.shared; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.io.ByteArrayOutputStream; public class MainActivity extends AppCompatActivity { Button share; TextView textView; ImageView imageView; final static int CROP_REQUEST_CODE = 3; final static int CAIJIAN = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initUI(); } private void initUI() { share = (Button)findViewById(R.id.share); textView = (TextView)findViewById(R.id.text); imageView = (ImageView)findViewById(R.id.image); share.setOnClickListener(new shareListener()); } /** * 调用手机系统分享 */ public class shareListener implements View.OnClickListener{ @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); /* 取得相片后返回本画面 */ startActivityForResult(intent, CAIJIAN); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Uri uri = data.getData(); // ContentResolver cr = this.getContentResolver(); Log.d("nsc", "requestCode:" + requestCode + ":" + resultCode); try { // Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); // imageView.setImageBitmap(bitmap); if (requestCode==CROP_REQUEST_CODE){ Bundle extras = data.getExtras(); if (extras!=null){ Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);//设置裁剪图片格式 imageView.setImageBitmap(photo);//显示裁剪图片 //将bitmap格式转成uri格式 Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo, null,null)); shared(uri);//分享裁剪图片 } } if (requestCode == CAIJIAN){ startImageZoom(data.getData());//裁剪图片 } } catch (Exception e) { Log.e("Exception", e.getMessage(),e); } super.onActivityResult(requestCode, resultCode, data); } /** * 分享图片 * @param uri */ public void shared(Uri uri){ 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())); } /** *裁剪图片 * @param uri */ private void startImageZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP");//调用Android系统自带的一个图片剪裁页面, intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true");// intent.putExtra("aspectX", 1);//进行修剪// aspectX aspectY 是宽高的比例 intent.putExtra("aspectY", 1);// intent.putExtra("outputX", 100);//outputX outputY 是裁剪图片宽高 intent.putExtra("outputY", 100); intent.putExtra("return-data", true); startActivityForResult(intent, CROP_REQUEST_CODE); } }实现效果: