Friday, July 5, 2019

How to Android Pick/Select images from gallery

Hello everybody, today the Code android example will introduce you to an Android tricks Pick images from the gallery, although very simple what if the contacts do not know, I will demo Android Select images from gallery for you, follow these steps.
Put this command line where you want to
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

Call onActivityResult() in class
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
         
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Creat file xml in have ImageView
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView" />
How to Android Pick/Select images from gallery

Tags: Android select image from gallery example, select images android, select photo in android, select photo android example, how to pick image example android

No comments:

Post a Comment