Android : Android Studio: display images from my android gallery

on Sunday, March 22, 2015


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


0 comments:

Post a Comment