Skip to content

Commit

Permalink
Update demo application
Browse files Browse the repository at this point in the history
  • Loading branch information
HBiSoft committed Apr 28, 2022
1 parent fd6b821 commit 0d904a7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 57 additions & 20 deletions app/src/main/java/com/hbisoft/pickitexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -66,16 +67,28 @@ 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);
pickitTitle = findViewById(R.id.pickitTitle);
}

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);
Expand All @@ -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);
}
}

Expand All @@ -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 {
Expand Down
25 changes: 19 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,35 @@
android:id="@+id/pickitTv"
android:visibility="invisible"/>

<RelativeLayout
<LinearLayout
android:id="@+id/button_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary">
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:weightSum="2">

<Button
android:id="@+id/button_pick"
android:id="@+id/button_pick_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
android:text="@string/pick_file"
android:text="Pick Video"
android:textColor="@android:color/white"
tools:text="@string/pick_file" />
android:layout_weight="1"
/>

</RelativeLayout>
<Button
android:id="@+id/button_pick_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
android:text="Pick Image"
android:textColor="@android:color/white"
android:layout_weight="1"
/>

</LinearLayout>

</RelativeLayout>

0 comments on commit 0d904a7

Please sign in to comment.