Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ You can customize something ...<br />
* `showTitle(Boolean) (default: true)`
* `setTitleBackgroundResId(R.color.xxx)`
* `setImageProvider(ImageProvider)`
* `setOnErrorListener(OnErrorListener onErrorListener)`
: If you want load grid image yourself, you can use your ImageProvider

#### Single Select
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/gun0912/tedbottompickerdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public void onImageSelected(final Uri uri) {
.setSelectedUri(selectedUri)
//.showVideoMedia()
.setPeekHeight(1200)
.setOnErrorListener(new TedBottomPicker.OnErrorListener() {
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
Toast.makeText(getBaseContext(), "Error : " + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.create();

bottomSheetDialogFragment.show(getSupportFragmentManager());
Expand Down Expand Up @@ -131,6 +138,13 @@ public void onImagesSelected(ArrayList<Uri> uriList) {
.setCompleteButtonText("Done")
.setEmptySelectionText("No Select")
.setSelectedUriList(selectedUriList)
.setOnErrorListener(new TedBottomPicker.OnErrorListener() {
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
Toast.makeText(getBaseContext(), "Error : " + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.create();

bottomSheetDialogFragment.show(getSupportFragmentManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.Locale;

import gun0912.tedbottompicker.adapter.GalleryAdapter;
import gun0912.tedbottompicker.exception.OnErrorNotImplementedException;
import gun0912.tedbottompicker.util.RealPathUtil;

public class TedBottomPicker extends BottomSheetDialogFragment {
Expand Down Expand Up @@ -282,7 +283,8 @@ public void onItemClick(View view, int position) {

GalleryAdapter.PickerTile pickerTile = imageGalleryAdapter.getItem(position);

switch (pickerTile.getTileType()) {
int tileType = pickerTile.getTileType();
switch (tileType) {
case GalleryAdapter.PickerTile.CAMERA:
startCameraIntent();
break;
Expand All @@ -295,7 +297,7 @@ public void onItemClick(View view, int position) {
break;

default:
errorMessage();
onError(new IllegalStateException("Unexpected type : " + tileType));
}

}
Expand Down Expand Up @@ -428,7 +430,7 @@ private void startCameraIntent() {
}

if (cameraInent.resolveActivity(getActivity().getPackageManager()) == null) {
errorMessage("This Application do not have Camera Application");
onError("This Application do not have Camera Application");
return;
}

Expand Down Expand Up @@ -478,7 +480,7 @@ private File getImageFile() {
cameraImageUri = Uri.fromFile(imageFile);
} catch (IOException e) {
e.printStackTrace();
errorMessage("Could not create imageFile for camera");
onError("Could not create imageFile for camera");
}


Expand Down Expand Up @@ -507,21 +509,21 @@ private File getVideoFile() {
cameraImageUri = Uri.fromFile(videoFile);
} catch (IOException e) {
e.printStackTrace();
errorMessage("Could not create imageFile for camera");
onError("Could not create imageFile for camera");
}


return videoFile;
}

private void errorMessage(String message) {
private void onError(String message) {
String errorMessage = message == null ? "Something wrong." : message;

if (builder.onErrorListener == null) {
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
} else {
builder.onErrorListener.onError(errorMessage);
}
onError(new RuntimeException(errorMessage));
}

private void onError(Throwable throwable) {
builder.onErrorListener.onError(throwable);
}

private void startGalleryIntent() {
Expand All @@ -537,7 +539,7 @@ private void startGalleryIntent() {
}

if (galleryIntent.resolveActivity(getActivity().getPackageManager()) == null) {
errorMessage("This Application do not have Gallery Application");
onError("This Application do not have Gallery Application");
return;
}

Expand All @@ -554,10 +556,6 @@ public void onActivityResult(int resultCode, Intent data) {
.startActivityForResult();
}

private void errorMessage() {
errorMessage(null);
}

private void setTitle() {

if (!builder.showTitle) {
Expand Down Expand Up @@ -611,7 +609,8 @@ private void onActivityResultGallery(Intent data) {
Uri temp = data.getData();

if (temp == null) {
errorMessage();
onError(new NullPointerException("Intent data is null"));
return;
}

String realPath = RealPathUtil.getRealPath(getActivity(), temp);
Expand All @@ -637,7 +636,7 @@ public interface OnImageSelectedListener {
}

public interface OnErrorListener {
void onError(String message);
void onError(Throwable throwable);
}

public interface ImageProvider {
Expand Down Expand Up @@ -683,9 +682,17 @@ public static class Builder implements Serializable {
ArrayList<Uri> selectedUriList;
Uri selectedUri;

private OnErrorListener defaultErrorListener = new OnErrorListener() {
@Override
public void onError(Throwable throwable) {
throw new OnErrorNotImplementedException("Error listener is not implemented", throwable);
}
};

public Builder(@NonNull Context context) {

this.context = context;
this.onErrorListener = defaultErrorListener;

setCameraTile(R.drawable.ic_camera);
setGalleryTile(R.drawable.ic_gallery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public GalleryAdapter(Context context, TedBottomPicker.Builder builder) {

} catch (Exception e) {
e.printStackTrace();
onError(e);
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
Expand Down Expand Up @@ -220,6 +221,9 @@ public interface OnItemClickListener {
public void onItemClick(View view, int position);
}

private void onError(Throwable throwable){
builder.onErrorListener.onError(throwable);
}

public static class PickerTile {

Expand Down Expand Up @@ -263,7 +267,7 @@ public String toString() {
} else if (isGalleryTile()) {
return "PickerTile";
} else {
return "Invalid item";
throw new IllegalStateException("Invalid item");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gun0912.tedbottompicker.exception;

public class OnErrorNotImplementedException extends RuntimeException {

public OnErrorNotImplementedException() {
super();
}

public OnErrorNotImplementedException(String message) {
super(message);
}

public OnErrorNotImplementedException(String message, Throwable cause) {
super(message, cause);
}

public OnErrorNotImplementedException(Throwable cause) {
super(cause);
}
}