From b36e85844d2591ca9982b8ecdd61ac2ff908dd20 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 15 Jun 2016 15:20:05 +0200 Subject: [PATCH] allow using the app as an image chooser --- app/src/main/AndroidManifest.xml | 6 ++++++ .../simplemobiletools/gallery/Constants.java | 1 + .../gallery/activities/MainActivity.java | 19 ++++++++++++++++++- .../gallery/activities/MediaActivity.java | 13 ++++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b017bb86a..3cc4ff78f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,12 @@ + + + + + + dirs; private int selectedItemsCnt; private Snackbar snackbar; @@ -318,11 +319,27 @@ public class MainActivity extends AppCompatActivity }); } + private boolean isPickIntent(Intent intent) { + return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null) { + final Intent result = new Intent(); + result.setData(data.getData()); + setResult(RESULT_OK, result); + finish(); + } + super.onActivityResult(requestCode, resultCode, data); + } + @Override public void onItemClick(AdapterView parent, View view, int position, long id) { final Intent intent = new Intent(this, MediaActivity.class); intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath()); - startActivity(intent); + intent.putExtra(Constants.PICK_INTENT, isPickIntent(getIntent())); + startActivityForResult(intent, PICK_IMAGE); } @Override diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index 0bd81f8c2..c68411d15 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -234,9 +234,16 @@ public class MediaActivity extends AppCompatActivity @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - final Intent intent = new Intent(this, ViewPagerActivity.class); - intent.putExtra(Constants.MEDIUM, media.get(position).getPath()); - startActivity(intent); + if (getIntent().getBooleanExtra(Constants.PICK_INTENT, false)) { + final Intent result = new Intent(); + result.setData(Uri.parse(media.get(position).getPath())); + setResult(RESULT_OK, result); + finish(); + } else { + final Intent intent = new Intent(this, ViewPagerActivity.class); + intent.putExtra(Constants.MEDIUM, media.get(position).getPath()); + startActivity(intent); + } } @Override