Maor Cohen
Maor Cohen

Reputation: 956

Android Studio: display images from my android gallery

I'm new about programming in Java and Android Studio. I'm trying to create a short app thanks to posts of other programmers here: an app with with a "Browse From Gallery" button -> when I click on it my android gallery is opened, then I choose an image and I would like it to be displayed on the imageView I created.

I succeeded to access my android gallery after pressing on the button and choose an image but nothing's shown on the imageView.

here's the main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".BlankActivityWithFragment" tools:ignore="MergeRootFrame">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Browse from Gallery"
        android:id="@+id/button"
        android:layout_gravity="left|top"
        android:onClick="onButtonClicked"
        android:enabled="true" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ffaaaa"
        android:id="@+id/imageViewGallery"
        android:layout_gravity="center_horizontal|top" />

</FrameLayout>

as you can see, Browse From Gallery's button will call onButtonClicked function.

here's my java code:

public class ImagesProj extends Activity {

    // define the variable that will show the gallery images
    ImageView imageViewGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images_proj);

        // connect the variable to the images_proj.xml
        imageViewGallery = (ImageView) findViewById(R.id.imageViewGallery);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    // This function is called after clicking the Browse From Gallery button
    public boolean onButtonClicked(View view) {

        // access the images gallery
        Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 2);

        return true;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            // String picturePath contains the path of selected Image

            // Show the Selected Image on ImageView
            ImageView imageView = (ImageView) findViewById(R.id.imageViewGallery);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }
}

please help me to display the chosen image, thanks!

the post I tried to look at: Picking a photo from gallery and show in a image view

Upvotes: 1

Views: 12548

Answers (1)

Karan
Karan

Reputation: 2130

It might be that image is very big and simply calling decodeFile on the path will not adjust the bitmap to the imageView.

Use a library (like most people :P) or look into ways of adjusting it to the dimensions of imageView. btw library will save you loads of effort but you will get better by only doing it yourself.

Upvotes: 0

Related Questions