diff --git a/.idea/misc.xml b/.idea/misc.xml index d5d35ec..a3a6d69 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/java/com/hbisoft/pickitexample/MainActivity.java b/app/src/main/java/com/hbisoft/pickitexample/MainActivity.java index 0653c49..db09267 100644 --- a/app/src/main/java/com/hbisoft/pickitexample/MainActivity.java +++ b/app/src/main/java/com/hbisoft/pickitexample/MainActivity.java @@ -44,8 +44,9 @@ public class MainActivity extends AppCompatActivity implements PickiTCallbacks { PickiT pickiT; //Views - Button button_pick; + Button button_pick_video, button_pick_image; TextView pickitTv, originalTv, originalTitle, pickitTitle; + String videoImageRef = ""; @Override protected void onCreate(Bundle savedInstanceState) { @@ -66,7 +67,8 @@ private void showLongToast(final String msg) { } private void init() { - button_pick = findViewById(R.id.button_pick); + button_pick_video = findViewById(R.id.button_pick_video); + button_pick_image = findViewById(R.id.button_pick_image); pickitTv = findViewById(R.id.pickitTv); originalTv = findViewById(R.id.originalTv); originalTitle = findViewById(R.id.originalTitle); @@ -74,8 +76,19 @@ private void init() { } private void buttonClickEvent() { - button_pick.setOnClickListener(view -> { - openGallery(); + button_pick_video.setOnClickListener(view -> { + videoImageRef = "video"; + openGallery("video"); + + // Make TextView's invisible + originalTitle.setVisibility(View.INVISIBLE); + originalTv.setVisibility(View.INVISIBLE); + pickitTitle.setVisibility(View.INVISIBLE); + pickitTv.setVisibility(View.INVISIBLE); + }); + button_pick_image.setOnClickListener(view -> { + videoImageRef = "image"; + openGallery("image"); // Make TextView's invisible originalTitle.setVisibility(View.INVISIBLE); @@ -85,24 +98,44 @@ private void buttonClickEvent() { }); } - private void openGallery() { + private void openGallery(String videoOrImage) { // first check if permissions was granted if (checkSelfPermission()) { - Intent intent; - if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { - intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI); - } else { - intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI); - } - // In this example we will set the type to video - intent.setType("video/*"); - intent.setAction(Intent.ACTION_GET_CONTENT); - intent.putExtra("return-data", true); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + if (videoImageRef.equals("video")) { + videoImageRef = ""; + Intent intent; + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI); + } else { + intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI); + } + // In this example we will set the type to video + intent.setType("video/*"); + intent.setAction(Intent.ACTION_GET_CONTENT); + intent.putExtra("return-data", true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + } + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + activityResultLauncher.launch(intent); + }else{ + videoImageRef = ""; + Intent intent; + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); + } else { + intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); + } + // In this example we will set the type to video + intent.setType("image/*"); + intent.setAction(Intent.ACTION_GET_CONTENT); + intent.putExtra("return-data", true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + } + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + activityResultLauncher.launch(intent); } - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - activityResultLauncher.launch(intent); } } @@ -122,7 +155,11 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis if (requestCode == PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permissions was granted, open the gallery - openGallery(); + if (videoImageRef.equals("video")) { + openGallery("video"); + }else{ + openGallery("image"); + } } // Permissions was not granted else { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 14f2275..c6f5bd5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -54,22 +54,35 @@ android:id="@+id/pickitTv" android:visibility="invisible"/> - + android:background="@color/colorPrimary" + android:orientation="horizontal" + android:weightSum="2">