diff --git a/matisse/src/main/java/com/zhihu/matisse/Matisse.java b/matisse/src/main/java/com/zhihu/matisse/Matisse.java index 0de59380a..aebc8b803 100644 --- a/matisse/src/main/java/com/zhihu/matisse/Matisse.java +++ b/matisse/src/main/java/com/zhihu/matisse/Matisse.java @@ -21,9 +21,11 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import com.zhihu.matisse.internal.entity.Item; import com.zhihu.matisse.ui.MatisseActivity; import java.lang.ref.WeakReference; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -85,6 +87,17 @@ public static List obtainResult(Intent data) { return data.getParcelableArrayListExtra(MatisseActivity.EXTRA_RESULT_SELECTION); } + /** + * Obtain user selected media path list in the starting Activity or Fragment. + * + * @param data Intent passed by {@link Activity#onActivityResult(int, int, Intent)} or + * {@link Fragment#onActivityResult(int, int, Intent)}. + * @return User selected media' {@link Item} list. + */ + public static ArrayList obtainListResult(Intent data) { + return data.getParcelableArrayListExtra(MatisseActivity.EXTRA_RESULT_SELECTION_ITEM); + } + /** * Obtain user selected media path list in the starting Activity or Fragment. * diff --git a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java index 66aeadfe3..e369e65f3 100644 --- a/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java +++ b/matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java @@ -29,6 +29,7 @@ import com.zhihu.matisse.engine.ImageEngine; import com.zhihu.matisse.filter.Filter; import com.zhihu.matisse.internal.entity.CaptureStrategy; +import com.zhihu.matisse.internal.entity.Item; import com.zhihu.matisse.internal.entity.SelectionSpec; import com.zhihu.matisse.listener.OnCheckedListener; import com.zhihu.matisse.listener.OnSelectedListener; @@ -364,6 +365,28 @@ public void forResult(int requestCode) { } } + /** + * Start to select media and wait for result. + * + * @param requestCode Identity of the request Activity or Fragment. + * @param selectionsList Last selections List + */ + public void forResult(int requestCode, ArrayList selectionsList) { + Activity activity = mMatisse.getActivity(); + if (activity == null) { + return; + } + + Intent intent = new Intent(activity, MatisseActivity.class); + intent.putExtra(MatisseActivity.EXTRA_REQUEST_SELECTION, selectionsList); + Fragment fragment = mMatisse.getFragment(); + if (fragment != null) { + fragment.startActivityForResult(intent, requestCode); + } else { + activity.startActivityForResult(intent, requestCode); + } + } + public SelectionCreator showPreview(boolean showPreview) { mSelectionSpec.showPreview = showPreview; return this; diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/model/SelectedItemCollection.java b/matisse/src/main/java/com/zhihu/matisse/internal/model/SelectedItemCollection.java index 7fdf69034..3985fe099 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/model/SelectedItemCollection.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/model/SelectedItemCollection.java @@ -76,6 +76,10 @@ public void setDefaultSelection(List uris) { mItems.addAll(uris); } + public void addSelections(List uris) { + mItems.addAll(uris); + } + public void onSaveInstanceState(Bundle outState) { outState.putParcelableArrayList(STATE_SELECTION, new ArrayList<>(mItems)); outState.putInt(STATE_COLLECTION_TYPE, mCollectionType); @@ -142,6 +146,10 @@ public List asList() { return new ArrayList<>(mItems); } + public ArrayList asArrayList() { + return new ArrayList<>(mItems); + } + public List asListOfUri() { List uris = new ArrayList<>(); for (Item item : mItems) { diff --git a/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java b/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java index 662379a2e..859f3d239 100644 --- a/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java +++ b/matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java @@ -73,7 +73,9 @@ public class MatisseActivity extends AppCompatActivity implements public static final String EXTRA_RESULT_SELECTION = "extra_result_selection"; public static final String EXTRA_RESULT_SELECTION_PATH = "extra_result_selection_path"; + public static final String EXTRA_RESULT_SELECTION_ITEM = "extra_result_selection_item"; public static final String EXTRA_RESULT_ORIGINAL_ENABLE = "extra_result_original_enable"; + public static final String EXTRA_REQUEST_SELECTION = "extra_request_selection"; private static final int REQUEST_CODE_PREVIEW = 23; private static final int REQUEST_CODE_CAPTURE = 24; public static final String CHECK_STATE = "checkState"; @@ -142,6 +144,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { if (savedInstanceState != null) { mOriginalEnable = savedInstanceState.getBoolean(CHECK_STATE); } + + ArrayList selections = getIntent().getParcelableArrayListExtra(EXTRA_REQUEST_SELECTION); + if(selections!=null && !selections.isEmpty()) { + mSelectedCollection.addSelections(selections); + } + updateBottomToolbar(); mAlbumsAdapter = new AlbumsAdapter(this, null, false); @@ -210,6 +218,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris); result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths); + result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, selected); result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable); setResult(RESULT_OK, result); finish(); @@ -322,6 +331,7 @@ public void onClick(View v) { result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris); ArrayList selectedPaths = (ArrayList) mSelectedCollection.asListOfString(); result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths); + result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, mSelectedCollection.asArrayList()); result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable); setResult(RESULT_OK, result); finish(); diff --git a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java index 9788a6014..9ae8d6275 100644 --- a/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java +++ b/sample/src/main/java/com/zhihu/matisse/sample/SampleActivity.java @@ -39,7 +39,9 @@ import com.zhihu.matisse.engine.impl.PicassoEngine; import com.zhihu.matisse.filter.Filter; import com.zhihu.matisse.internal.entity.CaptureStrategy; +import com.zhihu.matisse.internal.entity.Item; +import java.util.ArrayList; import java.util.List; public class SampleActivity extends AppCompatActivity implements View.OnClickListener { @@ -104,7 +106,7 @@ private void startAction(View v) { .setOnCheckedListener(isChecked -> { Log.e("isChecked", "onCheck: isChecked=" + isChecked); }) - .forResult(REQUEST_CODE_CHOOSE); + .forResult(REQUEST_CODE_CHOOSE,selections); break; case R.id.dracula: Matisse.from(SampleActivity.this) @@ -141,11 +143,15 @@ private void startAction(View v) { mAdapter.setData(null, null); } + private ArrayList selections = new ArrayList<>(); + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) { mAdapter.setData(Matisse.obtainResult(data), Matisse.obtainPathResult(data)); + selections.clear(); + selections.addAll(Matisse.obtainListResult(data)); Log.e("OnActivityResult ", String.valueOf(Matisse.obtainOriginalState(data))); } }