Last active
May 10, 2017 10:02
-
-
Save Kishanjvaghela/e356717e260073f571c2 to your computer and use it in GitHub Desktop.
ImagePicker
This file contains 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
private ImagePicker imagePicker; | |
// init | |
imagePicker = new ImagePicker(getActivity(), new ImageChooserListener() { | |
@Override | |
public void onImageChosen(final ChosenImage chosenImage) { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
filePath = chosenImage.getFileThumbnail(); | |
Picasso.with(getActivity()) | |
.load(new File(filePath)) | |
.resizeDimen(R.dimen.profileIconSize, R.dimen.profileIconSize) | |
.into(profileImageView); | |
} | |
}); | |
} | |
@Override | |
public void onError(final String reason) { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Log.i(TAG, "OnError: " + reason); | |
Utils.makeToast(getActivity(), reason); | |
} | |
}); | |
} | |
}); | |
// open | |
imagePicker.openDialog(); | |
//onActivityResult | |
imagePicker.onActivityResult(requestCode, resultCode, data); |
This file contains 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.content.DialogInterface; | |
import android.content.Intent; | |
import android.support.v7.app.AlertDialog; | |
import android.util.Log; | |
import com.kbeanie.imagechooser.api.ChooserType; | |
import com.kbeanie.imagechooser.api.ImageChooserListener; | |
import com.kbeanie.imagechooser.api.ImageChooserManager; | |
import jp.likenote.android.R; | |
public class ImagePicker { | |
public static final String TAG = "ImagePicker"; | |
private String filePath; | |
private int chooserType; | |
private ImageChooserManager imageChooserManager; | |
private Activity mActivity; | |
private ImageChooserListener imageChooserListener; | |
public ImagePicker(Activity activity, ImageChooserListener pickerListener) { | |
this.mActivity = activity; | |
this.imageChooserListener = pickerListener; | |
} | |
public void openDialog() { | |
String str[] = new String[]{mActivity.getResources().getString(R.string.text_open_camera), | |
mActivity.getResources().getString(R.string.text_choose_from_library)}; | |
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); | |
builder.setItems(str, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
performImgPicAction(which); | |
} | |
}).show(); | |
} | |
void performImgPicAction(int which) { | |
if (which == 1) { | |
chooserType = ChooserType.REQUEST_PICK_PICTURE; | |
} else { | |
chooserType = ChooserType.REQUEST_CAPTURE_PICTURE; | |
} | |
initializeImageChooser(chooserType); | |
try { | |
filePath = imageChooserManager.choose(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
Log.i(TAG, "OnActivityResult"); | |
Log.i(TAG, "File Path : " + filePath); | |
Log.i(TAG, "Chooser Type: " + chooserType); | |
if (resultCode == Activity.RESULT_OK | |
&& (requestCode == ChooserType.REQUEST_PICK_PICTURE || requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) { | |
if (imageChooserManager == null) { | |
initializeImageChooser(chooserType); | |
imageChooserManager.reinitialize(filePath); | |
} | |
imageChooserManager.submit(requestCode, data); | |
} | |
} | |
private void initializeImageChooser(int type) { | |
imageChooserManager = new ImageChooserManager(mActivity, type, true); | |
imageChooserManager.setImageChooserListener(imageChooserListener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment