Android show gallery with album - android

I would like to show an user experience like this:
to let user select images that he want... and then i'll use it.
How can i achieve this?
Thanks!

You can use intent to open default Gallery in Android and let user select image.
final static int RESULT_CHOOSE_IMAGE = 1;
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_CHOOSE_IMAGE);
After user selects image, onActivityResult() will be called which you will have to override as follows,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == RESULT_CHOOSE_IMAGE && data != null)
{
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 bitmapPath = cursor.getString(columnIndex); // path to user selected image
cursor.close();
// get bitmap from bitmap path
Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath , null);
}
}
Since bitmaps can be big in size, it may cause OutOfMemoryException while decoding from file. Refer this link for info regarding displaying bitmaps effeciently.
Android native gallery doesn't support multiple image selection by default, so you will have to use custom gallery for that purpose. See this link for tutorial .

In a future, try to be more specific in your questions so we can help you in a better way.
With the information you provided, i can just tell you that you could use a GridView to achieve what you want.
GridView Android Developers

Related

Android Select Multiple Images from gallery?

How can I select multiple images from gallery by using java.
I have Use this code but no error occurs
and can not able to select image from gallery.
Here is my code:
mainactivity.java
//button On Click:
{
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
//super.onActivityResult(requestCode,resultCode,data);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
/* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
}
}
}
});
It's not working. I can't select image.
#Drashti Kapadia :You can use Android open source library MultipleImagePick. And it uses Universal image loader library for asynchronous loading and caching.
Features and Benefits:
Select multiple images - Option to select multiple images from
Gallery and fast scroll over the Gallery
Maximum selection limit - Option to set maximum image selection
limit
Custom button title and error message - There is a option to
customize button title and error message
Method for scale down the bitmap - To avoid out of memory issue,
this module has in build bitmap scale down method
Callback methods - Success, error and cancel callback methods
Try the following code. Hope this helps!
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); // allow the user to select multiple items, only available in Android API 18 and higher.
startActivityForResult(intent, requestCode);

Selected Image content the correct path but cursor is null

I'm trying to display the photo just taken by camera and display it in a imageView. This is the method that has the problem:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;//data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
//managedQuery(selectedImage,filePathColumn, null, null, null);//
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
// The following three lines work perfectly :-)
// if I comment the part of the cursor lines above
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}
}
When debugging I see some values like for example
selectedImage : file:///mnt/sdcard/external_sd/Pictures/MyCameraApp/IMG_20150530_032752.jpg
and I think this is ok. Another interesting value is
filePathColumn : _data
is this value an expected one? You tell me, please.
So, cursor is null and the line
cursor.moveToFirst();
spits the error null pointer :-/. I'm debugging code in real device with Android 2.2. Help.
EDITION
this is the method that call the previous one
private void clickpic() {
// Check Camera
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// Open default camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
/*
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
*/
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create
//the getOutputMediaFileUri is implemented as saving media file suggests by developer.android.com
//intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the image capture Intent
startActivityForResult(intent,100);
} else {
Toast.makeText(MainActivity.this, "Camera not supported", Toast.LENGTH_LONG).show();
}
}
The commented code are my failed attempts.
You need to put the fileUri as an extra as shown here Android Developers
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
and try replacing your onActivityResult with:
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = fileUri;
Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}

display customized Phone gallery to pick multiple images

I am in trouble displaying customized phone gallery.
At first, I need to show images folders in gridview (such as phone gallery), and on selecting any of the folder, it must show the pictures inside it and should allow multiple selection, so that I can pick multiple pictures.
Is it achievable?
If yes, how to do that?
To open gallery :
//open gallery to choose image
private void captureImage() {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
When the image is selected from the gallery , the following function is invoked , implement it to save the image selected .
// When Image is selected from Gallery
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
// Get the Image's file name
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// successfully selected the image
// launching upload activity
tvCapturePicture.setText(fileName);
} else {
Toast.makeText(this, "You haven't picked any Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong ,please try again , ",
Toast.LENGTH_LONG).show();
}
}
as per your requirement follow this tutorial it will give ideas..
http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

Error while getting image from gallery in android

I know this this question was asked a lot but yeah it don't work for me :S
I normally tried to use it but there always error messages.
Here is the thing I tried to import:
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
{
switch(pMenuItem.getID())
{
case BACK:
createMenuChildScene();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
return true;
}
}
For some reasone the startActivityForResult is red underlined if I change the name of the onActivityResult to on ActivityForResult on the code down here it's still red underlined.
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
Over here the get Content resolver does not work or is undefined :/
I have no Idea what I'm doing wrong :/
You're trying image pick right?
the one you're using is outdated and works only for builds below KitKat.
i suggest you use document provider by google its faster and efficient and doest give OutOfMemory Errors.
https://developer.android.com/guide/topics/providers/document-provider.html
also for your startActivityForResult should work normally, try File->Invalidate Caches and Select Invalidate Caches and Restart it should resolve all the unnecessary errors. Good Luck!
Try this snippet.
In onActivityResult put this code when resultcode == Result_ok
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imageview.setImageBitmap(bitmap);
This solves your problem.

How to display the mobile gallery page in a custom view in android? [duplicate]

This question already has answers here:
populate gallery with images from web
(2 answers)
Closed 7 months ago.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
The above will take us to Gallery application which is there in our device,so we will be getting the Gallery screen which occupies the whole display of our device.But I want to display that Gallery application view in a Small Layout(something like I shown in below picture,instead of EditText,Buttons ,I want to get My device Gallery screen in that small layout).So that I can give user a feeling that he is not moving out of application.
I have seen this in one of the IPAD app (HelloAlbums).
IS it possible to achieve this in android?
I am using the above code to call phone gallery application.
But I want to display the gallery page in a custom view.
Suppose like in a view which I used below.
How can I achieve this?
please suggest
Thanks
try this
public void ChoosePicture(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bMap_image = BitmapFactory.decodeFile(filePath);
ImageView img = (ImageView) findViewById(R.id.gallery1);
img.setImageBitmap(bMap_image);
}catch(Exception e)
{}
}
}// resultCode
}// case 1
}// switch, request code
}

Categories

Resources