Picking image from phone gallery and using it in android app

September 12, 2012 | Rishabh Agrawal | General

The following code implements the picking of images from phone gallery and setting the image in a image view. The image is changed into bitmap format before using it in the image view.

Intent.ACTION_GET_CONTENT is called for selecting a image from gallery.

public class GalleryActivity extends Activity {

static Bitmap bmp;
ImageView GalImg;
protected static final int PHOTO_PICKED = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_screen);//setting layout xml screen
//The image is changed into bitmap format before using it in the image view.

// intent created for ACTION_GET_CONTENT for picking image from gallery
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);

//Set an explicit data type. This is used to create intents that only specify a type and not data
photoPickerIntent.setType(“image/*”);

/*Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode */
startActivityForResult(photoPickerIntent, PHOTO_PICKED);

}
/* called when activity exits*/

@Override
protected void onActivityResult(int requestCode, int resultcode,
Intent intent) {
super.onActivityResult(requestCode, resultcode, intent);
//if request code matches the code casino spiele online we specified
if (requestCode == 0) {

/*if intent is not null and resultcode is RESULT_OK(denotes operation succeeded)*/
if (intent != null && resultcode == RESULT_OK) {

// fetching uri of selected image
Uri selectedImage = intent.getData();
//string type data about image
String[] filePathColumn = { MediaStore.Images.Media.DATA};

/* Cursor provides random read-write access to the result set returned by a database query Query the given URI,
returning a Cursor over the result set. */

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
//Move the cursor to the first row.
cursor.moveToFirst();

/*The cursor query returns with the path, but you don”t know
which column it”s in until you use the columnIndex code.
That simply gets the number of the column based on its name,
the same one used in the filtering process.*/

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// file path of image
String filePath = cursor.getString(columnIndex);
cursor.close();

//Decode a file path into a bitmap
bmp = BitmapFactory.decodeFile(filePath);

//assigning image view id to GalImg image view
GalImg= (ImageView) findViewById(R.id.funny_imageView_id);
GalImg.setImageBitmap(scaledbmap);//setting image in image view

} else {
Log.i(“Status:”, “Photopicker canceled”);
}

}
}

Hope this example helps .


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal