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 .
Comments are closed.