allow using the gallery as a video chooser

This commit is contained in:
tibbi 2016-06-15 15:55:13 +02:00
parent 99a2ea6829
commit 08dd94f10f
4 changed files with 39 additions and 14 deletions

View File

@ -24,6 +24,12 @@
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/image"/> <data android:mimeType="vnd.android.cursor.dir/image"/>
</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/video"/>
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -4,4 +4,5 @@ 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_IMAGE_INTENT = "is_image_pick_intent"; public static final String PICK_IMAGE_INTENT = "is_image_pick_intent";
public static final String PICK_VIDEO_INTENT = "is_video_pick_intent";
} }

View File

@ -47,7 +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 static final int PICK_MEDIA = 2;
private List<Directory> dirs; private List<Directory> dirs;
private int selectedItemsCnt; private int selectedItemsCnt;
private Snackbar snackbar; private Snackbar snackbar;
@ -55,14 +55,16 @@ public class MainActivity extends AppCompatActivity
private List<String> toBeDeleted; private List<String> toBeDeleted;
private ActionMode actionMode; private ActionMode actionMode;
private Parcelable state; private Parcelable state;
private boolean isImagePickIntent; private boolean isPickImageIntent;
private boolean isPickVideoIntent;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);
isImagePickIntent = isImagePickIntent(getIntent()); isPickImageIntent = isPickImageIntent(getIntent());
isPickVideoIntent = isPickVideoIntent(getIntent());
} }
@Override @Override
@ -137,9 +139,12 @@ public class MainActivity extends AppCompatActivity
final Map<String, Directory> directories = new LinkedHashMap<>(); final Map<String, Directory> directories = new LinkedHashMap<>();
final List<String> invalidFiles = new ArrayList<>(); final List<String> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (isPickVideoIntent && i == 0)
continue;
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (i == 1) { if (i == 1) {
if (isImagePickIntent) if (isPickImageIntent)
continue; continue;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
@ -324,14 +329,21 @@ public class MainActivity extends AppCompatActivity
}); });
} }
private boolean isImagePickIntent(Intent intent) { private boolean isPickImageIntent(Intent intent) {
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && return isPickIntent(intent) && intent.getData().equals(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.getData().equals(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); }
private boolean isPickVideoIntent(Intent intent) {
return isPickIntent(intent) && intent.getData().equals(MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
}
private boolean isPickIntent(Intent intent) {
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && intent.getData() != null;
} }
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null) { if (requestCode == PICK_MEDIA && resultCode == RESULT_OK && data != null) {
final Intent result = new Intent(); final Intent result = new Intent();
result.setData(data.getData()); result.setData(data.getData());
setResult(RESULT_OK, result); setResult(RESULT_OK, result);
@ -344,8 +356,9 @@ public class MainActivity extends AppCompatActivity
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());
intent.putExtra(Constants.PICK_IMAGE_INTENT, isImagePickIntent); intent.putExtra(Constants.PICK_IMAGE_INTENT, isPickImageIntent);
startActivityForResult(intent, PICK_IMAGE); intent.putExtra(Constants.PICK_VIDEO_INTENT, isPickVideoIntent);
startActivityForResult(intent, PICK_MEDIA);
} }
@Override @Override

View File

@ -47,14 +47,16 @@ public class MediaActivity extends AppCompatActivity
private boolean isSnackbarShown; private boolean isSnackbarShown;
private List<String> toBeDeleted; private List<String> toBeDeleted;
private Parcelable state; private Parcelable state;
private boolean isImagePickIntent; private boolean isPickImageIntent;
private boolean isPickVideoIntent;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media); setContentView(R.layout.activity_media);
ButterKnife.bind(this); ButterKnife.bind(this);
isImagePickIntent = getIntent().getBooleanExtra(Constants.PICK_IMAGE_INTENT, false); isPickImageIntent = getIntent().getBooleanExtra(Constants.PICK_IMAGE_INTENT, false);
isPickVideoIntent = getIntent().getBooleanExtra(Constants.PICK_VIDEO_INTENT, false);
} }
@Override @Override
@ -110,9 +112,12 @@ public class MediaActivity extends AppCompatActivity
final List<Medium> myMedia = new ArrayList<>(); final List<Medium> myMedia = new ArrayList<>();
final List<String> invalidFiles = new ArrayList<>(); final List<String> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (isPickVideoIntent && i == 0)
continue;
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (i == 1) { if (i == 1) {
if (isImagePickIntent) if (isPickImageIntent)
continue; continue;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
@ -239,7 +244,7 @@ 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 (isImagePickIntent) { if (isPickImageIntent || isPickVideoIntent) {
final Intent result = new Intent(); final Intent result = new Intent();
result.setData(Uri.parse(media.get(position).getPath())); result.setData(Uri.parse(media.get(position).getPath()));
setResult(RESULT_OK, result); setResult(RESULT_OK, result);