package com.moral.sharepicturesdemo; import android.Manifest; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.util.ArrayList; import java.util.List; import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity; import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerPreviewActivity; import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.EasyPermissions; public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks, BGASortableNinePhotoLayout.Delegate { private static final int REQUEST_CODE_PERMISSION_PHOTO_PICKER = 1; private static final int REQUEST_CODE_CHOOSE_PHOTO = 1; private static final int REQUEST_CODE_PHOTO_PREVIEW = 2; /** * 拖拽排序九宫格控件 */ private BGASortableNinePhotoLayout mPhotosSnpl; private EditText mContentEt; private TextView tv_moment_add_choice_photo,tv_moment_add_publish; private ArrayList imgList = new ArrayList<>(); private ArrayList imgFilesList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentEt = (EditText) findViewById(R.id.et_moment_add_content); mPhotosSnpl = (BGASortableNinePhotoLayout) findViewById(R.id.snpl_moment_add_photos); tv_moment_add_choice_photo = (TextView) findViewById(R.id.tv_moment_add_choice_photo); tv_moment_add_publish = (TextView) findViewById(R.id.tv_moment_add_publish); tv_moment_add_choice_photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { choicePhotoWrapper(); } }); tv_moment_add_publish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Intent intent = new Intent(); // ComponentName comp = new ComponentName("com.tencent.mm", // "com.tencent.mm.ui.tools.ShareToTimeLineUI"); // intent.setComponent(comp); // intent.setAction(Intent.ACTION_SEND_MULTIPLE); // intent.setType("image/*"); // // ArrayList imageUris = new ArrayList(); // for (File f : imgFilesList) { // imageUris.add(Uri.fromFile(f)); // } // intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); // intent.putExtra("Kdescription", "wwwwwwwwwwwwwwwwwwww"); // startActivity(intent); Intent localIntent=new Intent(Intent.ACTION_SEND_MULTIPLE); localIntent.putExtra("Kdescription", "我的分享"); localIntent.putExtra(Intent.EXTRA_SUBJECT, "分享"); localIntent.putExtra(Intent.EXTRA_TEXT, "你好 "); localIntent.putExtra(Intent.EXTRA_TITLE, "我是标题"); localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ArrayList imageUris = new ArrayList(); for (File f : imgFilesList) { imageUris.add(Uri.fromFile(f)); } localIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); localIntent.setType("image/*"); startActivity(Intent.createChooser(localIntent, "分享")); } }); mPhotosSnpl.setDelegate(this); } @AfterPermissionGranted(REQUEST_CODE_PERMISSION_PHOTO_PICKER) private void choicePhotoWrapper() { String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}; if (EasyPermissions.hasPermissions(this, perms)) { // 拍照后照片的存放目录,改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能 File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerTakePhoto"); startActivityForResult(BGAPhotoPickerActivity.newIntent(this, takePhotoDir, mPhotosSnpl.getMaxItemCount() - mPhotosSnpl.getItemCount(), null, false), REQUEST_CODE_CHOOSE_PHOTO); } else { EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片\n\n2.拍照", REQUEST_CODE_PERMISSION_PHOTO_PICKER, perms); } } @Override public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList models) { choicePhotoWrapper(); } @Override public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList models) { mPhotosSnpl.removeItem(position); } @Override public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList models) { startActivityForResult(BGAPhotoPickerPreviewActivity.newIntent(this, mPhotosSnpl.getMaxItemCount(), models, models, position, false), REQUEST_CODE_PHOTO_PREVIEW); } @Override public void onPermissionsGranted(int requestCode, List perms) { } @Override public void onPermissionsDenied(int requestCode, List perms) { if (requestCode == REQUEST_CODE_PERMISSION_PHOTO_PICKER) { Toast.makeText(this, "您拒绝了「图片选择」所需要的相关权限!", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_PHOTO) { imgList.clear(); imgList.addAll(BGAPhotoPickerActivity.getSelectedImages(data)); mPhotosSnpl.addMoreData(imgList); } else if (requestCode == REQUEST_CODE_PHOTO_PREVIEW) { imgList.clear(); imgList.addAll(BGAPhotoPickerActivity.getSelectedImages(data)); mPhotosSnpl.setData(imgList); } for (int i=0;i