张海江
2016-12-21 12ff20edef42257e8046e41ae09b6479549ee7f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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<String> imgList = new ArrayList<>();
    private ArrayList<File> 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<Uri> imageUris = new ArrayList<Uri>();
//                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<Uri> imageUris = new ArrayList<Uri>();
                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<String> models) {
        choicePhotoWrapper();
    }
 
    @Override
    public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        mPhotosSnpl.removeItem(position);
    }
 
    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        startActivityForResult(BGAPhotoPickerPreviewActivity.newIntent(this, mPhotosSnpl.getMaxItemCount(), models, models, position, false), REQUEST_CODE_PHOTO_PREVIEW);
    }
 
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
 
    }
 
    @Override
    public void onPermissionsDenied(int requestCode, List<String> 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<imgList.size();i++){
            File file = new File(imgList.get(i));
            imgFilesList.add(file);
            Log.d("haijiang",file.getAbsolutePath());
        }
    }
}