Created
August 21, 2018 13:56
-
-
Save ngima/aa50114bf860ba368a604b2b2a6343a5 to your computer and use it in GitHub Desktop.
Android Image Picker
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
@Override | |
protected void onActivityResult(final int requestCode, int resultCode, final Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case REQUEST_TAKE_PICTURE: | |
try { | |
Uri uri = getImageUri(Profile.this, ((Bitmap) data.getExtras().get("data"))); | |
if (uri != null) { | |
InputStream iStream = getContentResolver().openInputStream(uri); | |
byte[] inputData = getBytes(iStream); | |
params.put("file", new DataPart(Calendar.getInstance().getTimeInMillis() + ".jpg", inputData, "image/jpeg")); | |
} | |
} catch (FileNotFoundException e) { | |
} catch (IOException e) { | |
} | |
break; | |
case REQUEST_CHOOSE_PICTURE: | |
try { | |
Uri uri = data.getData(); | |
if (uri != null) { | |
InputStream iStream = getContentResolver().openInputStream(uri); | |
byte[] inputData = getBytes(iStream); | |
params.put("file", new DataPart(Calendar.getInstance().getTimeInMillis() + ".jpg", inputData, "image/jpeg")); | |
} | |
} catch (FileNotFoundException e) { | |
} catch (IOException e) { | |
} | |
break; | |
} | |
} | |
public void showImageChooserOption(){ | |
ChooseSetProfilePhotoOptionsDialog dialog = ChooseSetProfilePhotoOptionsDialog.newInstance(new ChooseSetProfilePhotoOptionsDialog.Callback() { | |
@Override | |
public void newPicture() { | |
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
startActivityForResult(takePicture, REQUEST_TAKE_PICTURE); | |
} | |
@Override | |
public void selectPicture() { | |
Intent pickPhoto = new Intent(Intent.ACTION_PICK, | |
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(pickPhoto, REQUEST_CHOOSE_PICTURE); | |
} | |
}); | |
dialog.show(getSupportFragmentManager(), ChooseSetProfilePhotoOptionsDialog.TAG); | |
} | |
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.graphics.Color; | |
import android.graphics.drawable.ColorDrawable; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.DialogFragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import android.view.animation.LinearInterpolator; | |
import android.view.animation.RotateAnimation; | |
public class ChooseSetProfilePhotoOptionsDialog extends DialogFragment { | |
public static final String TAG = "ChooseSetProfilePhotoOptionsDialog"; | |
Callback callback; | |
public static ChooseSetProfilePhotoOptionsDialog newInstance(Callback callback) { | |
ChooseSetProfilePhotoOptionsDialog dialog = new ChooseSetProfilePhotoOptionsDialog(); | |
dialog.callback = callback; | |
return dialog; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
try { | |
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); | |
} catch (Exception e) { | |
// | |
} | |
return inflater.inflate(R.layout.dialog_choose_profile_photo_options, container, false); | |
} | |
@Override | |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | |
view.findViewById(R.id.txt_take_new_picture).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
callback.newPicture(); | |
dismiss(); | |
} | |
}); | |
view.findViewById(R.id.txt_choose_picture).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
callback.selectPicture(); | |
dismiss(); | |
} | |
}); | |
} | |
interface Callback { | |
void newPicture(); | |
void selectPicture(); | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:cardCornerRadius="@dimen/spacing_8"> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="@dimen/spacing_16" | |
android:text="@string/txt_set_a_profile_photo" | |
android:textAppearance="@style/TextAppearance.AppCompat.Medium" | |
android:textColor="@color/black" /> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="1dp" | |
android:background="@color/light_grey_50" /> | |
<TextView | |
android:id="@+id/txt_take_new_picture" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:clickable="true" | |
android:foreground="?android:selectableItemBackground" | |
android:padding="@dimen/spacing_24" | |
android:text="@string/txt_take_a_picture" | |
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> | |
<TextView | |
android:id="@+id/txt_choose_picture" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:clickable="true" | |
android:foreground="?android:selectableItemBackground" | |
android:padding="@dimen/spacing_24" | |
android:text="@string/txt_select_picture" | |
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> | |
</LinearLayout> | |
</android.support.v7.widget.CardView> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment