allow using the app as an image chooser

This commit is contained in:
tibbi 2016-06-15 15:20:05 +02:00
parent fd01e7dca5
commit b36e85844d
4 changed files with 35 additions and 4 deletions

View file

@ -18,6 +18,12 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/image"/>
</intent-filter>
</activity> </activity>
<activity <activity

View file

@ -3,4 +3,5 @@ package com.simplemobiletools.gallery;
public class Constants { public class Constants {
public static final String DIRECTORY = "directory"; public static final String DIRECTORY = "directory";
public static final String MEDIUM = "medium"; public static final String MEDIUM = "medium";
public static final String PICK_INTENT = "is_pick_intent";
} }

View file

@ -47,6 +47,7 @@ public class MainActivity extends AppCompatActivity
@BindView(R.id.directories_grid) GridView gridView; @BindView(R.id.directories_grid) GridView gridView;
private static final int STORAGE_PERMISSION = 1; private static final int STORAGE_PERMISSION = 1;
private static final int PICK_IMAGE = 2;
private List<Directory> dirs; private List<Directory> dirs;
private int selectedItemsCnt; private int selectedItemsCnt;
private Snackbar snackbar; 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 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(this, MediaActivity.class); final Intent intent = new Intent(this, MediaActivity.class);
intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath()); intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath());
startActivity(intent); intent.putExtra(Constants.PICK_INTENT, isPickIntent(getIntent()));
startActivityForResult(intent, PICK_IMAGE);
} }
@Override @Override

View file

@ -234,10 +234,17 @@ public class MediaActivity extends AppCompatActivity
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
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); final Intent intent = new Intent(this, ViewPagerActivity.class);
intent.putExtra(Constants.MEDIUM, media.get(position).getPath()); intent.putExtra(Constants.MEDIUM, media.get(position).getPath());
startActivity(intent); startActivity(intent);
} }
}
@Override @Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {