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);
Related
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 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/
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
}
How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?
The following code gives a list of activities that can select an image:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
List<ResolveInfo> infos = activity.getPackageManager().queryIntentActivities(intent, 0);
If I then do something like this:
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE);
then if the user has more than one application that can act like a gallery (such as ASTRO File Manager), s/he is prompted to select one of them, with no "set as default" option.
I don't want the user to be prompted to choose between them each time, so I'd like to just use the native gallery.
The hacky code sample below uses a whitelist to test for known native gallery names:
for (ResolveInfo info : infos) {
if ( 0==info.activityInfo.name.compareTo("com.cooliris.media.Gallery")
|| 0==info.activityInfo.name.compareTo("com.htc.album.CollectionsActivity")
) {
// found the native gallery
doSomethingWithNativeGallery();
}
}
Feels kinda dirty. Is there a better way? I suspect I'm missing something in my intent.
I didn't get last part of your code.
To start the native gallry what i did is -
public void upload(){
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/jpg");
photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
startActivityForResult(photoPickerIntent, 1);
}
Call the upload() where you like to start the native gallery.
Then to get that image info i did -
/**
* Retrieves the returned image from the Intent, inserts it into the MediaStore, which
* automatically saves a thumbnail. Then assigns the thumbnail to the ImageView.
* #param requestCode is the sub-activity code
* #param resultCode specifies whether the activity was cancelled or not
* #param intent is the data packet passed back from the sub-activity
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
return;
}
else if(resultCode == RESULT_OK) {
Uri uri = intent.getData();
String path = getPath(uri);
Log.i("PATH", path);
data = path;
return;
}
uplad();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
If you wouldn't get your answer, clear what you like to do