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/
Related
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);
I have a function which tries to get the image from the image path, it works depending on were the image is.
For some reason, it works fine from this location
/storage/extSdCard/DCIM/100MEDIA/IMAG0082.jpg
but these locations does not work
/storage/extSdCard/DCIM/Camera/20150315_133312.jpg
/storage/emulated/0/Pictures/20150825_161105.jpg
It results in a blank thumbnail and an error when i try to upload the image from file path.
Here is my Intent for picking the photo:
//Camera Application for existing Photo
public void btnExistingPhotoClicked(View v) {
//invoke the intent to get an image from the phone
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
}
And here is the actual onActivityResult which picks it up, put it in a thumbnail and then display the imagepath in a Textview
case 1:
if(resultCode == RESULT_OK){
//function for a selected image
Uri selectedImage = imageReturnedIntent.getData();
mImageView.setImageURI(selectedImage);
Toast.makeText(this, mImageView.toString(), Toast.LENGTH_LONG).show();
Log.d("MainActivity", mImageView.toString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgurl = cursor.getString(columnIndex);
cursor.close();
//The imageview is set, this is the small preview in the app
TextView tximgurl = (TextView) findViewById(R.id.txtImgUrl);
tximgurl.setText(imgurl);
Log.d("MainActivity", imgurl.toString());
//Toast.makeText(this, imgurl, Toast.LENGTH_LONG).show();
}
break;
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);
}
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
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
}