Browse existing image or capture new image in android

In this we will learn about how to select exist image in gallery . And In this we will learn about how to capture image from the android device and save into internal memory of android device. In this we do not need external memory or sdcard for capture image.


1.) Intent browsePicture = new Intent();
browsePicture.setType("image/*");
browsePicture.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(browsePicture, "Select Picture"),SELECT_PICTURE);

by calling this intent it opens options to the user to select existing image from gallery and etc.

2.) Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

by calling this intent it opens camera of android devices.


3.) FileOutputStream fos =openFileOutput("demo.jpg",Context.MODE_WORLD_WRITEABLE);
fos.close();
File f = new File(getFilesDir() + File.separator + "demo.jpg");

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePicture, CAPTURE_PICTURE);

In this we write captured images into internal memory in android devices.


4.) String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;

In this we get the path of saved capture image.

0 comments:

Post a Comment