123ArticleOnline Logo
Welcome to 123ArticleOnline.com!

ALL >> Technology,-Gadget-and-Science >> View Article

Single Or Multiple Image Selection From Device Gallery It Through Image Views In Xamarin Android

By Author: Rosina DE Palma
Total Articles: 1

In this tutorial you will learn how to select images from your device gallery and show it to your required places. You can pick single or multiple images according to your requirement. One thing need to be remembered that multiple image selection option is only available in Android API level 18 or higher. So for this reason in your project Manifst file the minimum SDK version should be 18 or higher.

Purpose
Single or multiple image selection from your device gallery and show it through Imageviews in Xamarin Android.

Scope
IDE: Visual Studio Xamarin or Xamarin Studio.

Procedure
At first create an project in xamarin.android. Design Main.axml layout with four imageview as displayed in below screen with a simple button. This button click event will help to get images from device gallery and will show that images in this perticular imageviews.You can customize your view and code as per your requirements.


Step 1:

In your Main.axml layout give this following design code







In your designer screen you will get this design view:


Step 2:

Next you need to set permission for access device gallery. Write this permission code to your Android manifest file:




Step 3:

Next go to Mainactivity.cs file.Set the MainLauncher = true for this activity. Set the contentview as Main.axml layout and define all your images and button as required.

public static readonly int PickImageId = 1000;
Button btnupload;
ImageView profimg1, profimg2, profimg3, profimg4;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);

btnupload = FindViewById(Resource.Id.btniploadpic);
profimg1 = FindViewById(Resource.Id.profimg1);
profimg2 = FindViewById(Resource.Id.profimg2);
profimg3 = FindViewById(Resource.Id.profimg3);
profimg4 = FindViewById(Resource.Id.profimg4);

btnupload.Click += (sender, e) =>
{
}
}

Step 4:

Now in button click event, call the intent of image types and set CreateChooser. If you call Intent.createChooser(), passing it your Intent object, it returns a version of your intent that will always display the chooser.

Following is the source code:

Intent intent= new Intent();
intent.SetType("image/*");
intent.PutExtra(Intent.ExtraAllowMultiple, true);
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), PickImageId);


• Note that here we used Intent.ExtraAllowMultiple true, which means you can select single image as well as multiple images from gallery file.

Following is the output of this above code:


Step 5:

Next we need to override another function which is OnActivityResult. We will get all the selected images through this override method's intent data. Here's a function that invokes an intent to get all photos from gallery or any other App that we have selected from createchooser.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{

if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{

ClipData clipData = data.ClipData;
if (clipData != null) {
for (int i = 0; i < clipData.ItemCount; i++) {
ClipData.Item item = clipData.GetItemAt(i);
Uri uri = item.Uri;
if (i==0)
{
profimg1.SetImageURI(uri);
}
else if (i==1)
{
profimg2.SetImageURI(uri);
}
else if(i==2)
{
profimg3.SetImageURI(uri);
}
else if(i==3)
{
profimg4.SetImageURI(uri);
} }
}

//Uri uri = data.Data; for single images
//profimg1.SetImageURI(uri);
}


Explanation:

If we get any result from the above mentioned method then we need to call Clipdata class which contain all the images URI. Then according to the 'i' position we set the imageview's imagesource. Now its time to Run your project and you will see output as follows:

As we have taken four imageviews we are able to display only four images. You can use gridview as well to display multiple images in a grid.

Step 6:

If you want to display only single images then don't use clipdata class.Instead of using clipdata class use this following code for single image upload:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{

if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
Uri uri = data.Data;
profimg1.SetImageURI(uri);//Your imageview
}
}

Now its time to Run your project and you will see output as follows:

Step 7:

We can switch image positions in image click events easily.
Here is an small example of it.You can customize it as your own. In this example we have changed first imageview positions with other imageviews. Here is a sample source code:

profimg2.Click += (sender, e) =>
{
var image1 = profimg2.Drawable;
var image2 = profimg1.Drawable;
profimg2.SetImageDrawable(image2);
profimg1.SetImageDrawable(image1);
};
profimg3.Click += (sender, e) =>
{
var image1 = profimg3.Drawable;
var image2 = profimg1.Drawable;
profimg3.SetImageDrawable(image2);
profimg1.SetImageDrawable(image1);
};
profimg4.Click += (sender, e) =>
{
var image1 = profimg4.Drawable;
var image2 = profimg1.Drawable;
profimg4.SetImageDrawable(image2);
profimg1.SetImageDrawable(image1);
};

Run your project and you will see output as follows:


Conclusion:

So from above explanation it is clear how we can access images from different Apps and show it to our App(single or multiple images).Also how we can switch images position in imageview click events.

Total Views: 53Word Count: 2215See All articles From Author

Technology, Gadget and Science Articles

1. Relevancy In The Modern Day Scenario
Author: Education Portal

2. Hire Pool Builders
Author: Elena Southcott

3. Various Features To Look For In Contract Management Software
Author: James Blake

4. Vitamins: How Do They Work?
Author: James Brown

5. Security Solutions To Home, Schools And Business
Author: Vivency Global

6. Blackberry Leap Screen Replacement In The Uk
Author: Blackberryrepairer

7. How To Drive Smart Customized Consumer Engagement Using Hybris Marketing
Author: Sandeep Arora

8. Nokia Lumia Screen Replacement In The Uk
Author: Nokiarepairer

9. Global Cloud Radio Access Network (c-ran) Market Trend
Author: kusum

10. Kotlin On Android: A Step Ahead In Coding
Author: Eldon Broady

11. Hp Laptop Repair Services In The Uk
Author: Hprepairer

12. How To Troubleshoot The Activation Errors Of The Mcafee Antivirus Subscribed With Cd Or Retail Card?
Author: Robert Williams

13. Videocon Wallcam Launches Mobile Vehicle Surveillance Solution
Author: Videoocn WallCam

14. Industrial Box Type Oven
Author: digiqualsystems

15. Benefits Of Hiring Smart Av (audio Visual) Equipments For Your Conference
Author: One World Rental

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: