Last active
June 17, 2016 10:59
-
-
Save VAdaihiep/3c4ecf0b8cf89fa7b565aa47685da9e0 to your computer and use it in GitHub Desktop.
Pick image or capture simplest way by custom 'com.kbeanie:image-chooser-library:1.5.8@aar'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import com.kbeanie.imagechooser.api.ChooserType; | |
import com.kbeanie.imagechooser.api.ChosenImage; | |
import com.kbeanie.imagechooser.api.ChosenImages; | |
import com.kbeanie.imagechooser.api.ImageChooserListener; | |
import com.kbeanie.imagechooser.api.ImageChooserManager; | |
/** | |
* Usage: Must call ImagePickerHelper.onActivityResult(), onSaveInstanceState, onRestoreInstanceState | |
* To pick or capture image, just call showDialogPickImageFrom(). | |
* Created by [email protected] on 17/06/2016. | |
*/ | |
public class ImagePickerHelper implements ImageChooserListener{ | |
private Activity activity; | |
private OnChosenImage onChosenImage; | |
private boolean isActivityResultOver = false; | |
private int chooserType; | |
private ImageChooserManager imageChooserManager; | |
private String imagePath; | |
public ImagePickerHelper(Activity activity, OnChosenImage pOnChosenImage) { | |
this.onChosenImage = pOnChosenImage; | |
this.activity = activity; | |
} | |
public ImagePickerHelper(Activity activity) { | |
this(activity, null); | |
} | |
public String getImagePath() { | |
return imagePath; | |
} | |
public OnChosenImage getOnChosenImage() { | |
return onChosenImage; | |
} | |
public void setOnChosenImage(OnChosenImage onChosenImage) { | |
this.onChosenImage = onChosenImage; | |
} | |
/** | |
* | |
*/ | |
public void showDialogPickImageFrom() { | |
AlertDialog.Builder builder = new AlertDialog.Builder(activity); | |
builder.setTitle(R.string.pick_image_from) | |
.setPositiveButton(R.string.gallery, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
pickImage(); | |
} | |
}) | |
.setNegativeButton(R.string.take_photo, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
captureImage(); | |
} | |
}); | |
builder.show(); | |
} | |
/** | |
* | |
*/ | |
public void pickImage() { | |
chooserType = ChooserType.REQUEST_PICK_PICTURE; | |
imageChooserManager = new ImageChooserManager(activity, | |
ChooserType.REQUEST_PICK_PICTURE, true); | |
imageChooserManager.setImageChooserListener(this); | |
imageChooserManager.clearOldFiles(); | |
try { | |
imagePath = imageChooserManager.choose(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* | |
*/ | |
public void captureImage() { | |
chooserType = ChooserType.REQUEST_CAPTURE_PICTURE; | |
imageChooserManager = new ImageChooserManager(activity, | |
ChooserType.REQUEST_CAPTURE_PICTURE, true); | |
imageChooserManager.setImageChooserListener(this); | |
try { | |
imagePath = imageChooserManager.choose(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void reinitializeImageChooser() { | |
imageChooserManager = new ImageChooserManager(activity, chooserType, true); | |
imageChooserManager.setImageChooserListener(this); | |
imageChooserManager.reinitialize(imagePath); | |
} | |
/** | |
* Must call on Activity.onActivityResult() | |
* | |
* @param requestCode | |
* @param resultCode | |
* @param data | |
*/ | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (resultCode == Activity.RESULT_OK && | |
(requestCode == ChooserType.REQUEST_PICK_PICTURE || requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) { | |
if (imageChooserManager == null) { | |
reinitializeImageChooser(); | |
} | |
imageChooserManager.submit(requestCode, data); | |
} | |
} | |
public void onSaveInstanceState(Bundle outState) { | |
outState.putBoolean("activity_result_over", isActivityResultOver); | |
outState.putInt("chooser_type", chooserType); | |
outState.putString("media_path", imagePath); | |
} | |
public void onRestoreInstanceState(Bundle savedInstanceState) { | |
if (savedInstanceState != null) { | |
if (savedInstanceState.containsKey("chooser_type")) { | |
chooserType = savedInstanceState.getInt("chooser_type"); | |
} | |
if (savedInstanceState.containsKey("media_path")) { | |
imagePath = savedInstanceState.getString("media_path"); | |
} | |
if (savedInstanceState.containsKey("activity_result_over")) { | |
isActivityResultOver = savedInstanceState.getBoolean("activity_result_over"); | |
} | |
} | |
if (isActivityResultOver) { | |
if(onChosenImage != null) { | |
onChosenImage.onChosen(imagePath); | |
} | |
} | |
} | |
@Override | |
public void onImageChosen(ChosenImage chosenImage) { | |
isActivityResultOver = true; | |
imagePath = chosenImage.getFileThumbnail(); | |
if (onChosenImage != null) { | |
onChosenImage.onChosen(imagePath); | |
} | |
} | |
@Override | |
public void onError(String s) { | |
} | |
@Override | |
public void onImagesChosen(ChosenImages chosenImages) { | |
} | |
/** | |
* Callback for public usage | |
*/ | |
public interface OnChosenImage { | |
public void onChosen(String imagePath); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity implements ImagePickerHelper.OnChosenImage { | |
ImagePickerHelper imagePickerHelper; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
imagePickerHelper = new ImagePickerHelper(this, this); | |
findViewById(R.id.buttonPick).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
imagePickerHelper.showDialogPickImageFrom(); | |
} | |
}); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
imagePickerHelper.onActivityResult(requestCode, resultCode, data); | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
imagePickerHelper.onSaveInstanceState(outState); | |
super.onSaveInstanceState(outState); | |
} | |
@Override | |
protected void onRestoreInstanceState(Bundle savedInstanceState) { | |
imagePickerHelper.onRestoreInstanceState(savedInstanceState); | |
super.onRestoreInstanceState(savedInstanceState); | |
} | |
@Override | |
public void onChosen(final String imagePath) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
//TODO Show image from imagePath | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment