android pick images from gallery
Absolutely. Try this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Don't forget also to create the constant PICK_IMAGE, so you can recognize when the user comes back from the image gallery Activity:
public static final int PICK_IMAGE = 1;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE) {
//TODO: action
}
}
That's how I call the image gallery. Put it in and see if it works for you.
EDIT:
This brings up the Documents app. To allow the user to also use any gallery apps they might have installed:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
What's the way to pick images from gallery on Android in 2018?
The best way is to use the Android Storage Access Framework (https://developer.android.com/guide/topics/providers/document-provider.html).
For instance, in your activity you can request to the device and type of document (notice the intent.setType method, if you want to filter by document type).
This code will open the android document picker and let user to pick an image:
public class MainActivity extends AppCompatActivity {
private static final int OPEN_DOCUMENT_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.floatingActionButton).setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, OPEN_DOCUMENT_CODE);
});
}
}
Then, just implement the onActivityResult in the very same activity, in order to receive the image selected:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == OPEN_DOCUMENT_CODE && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
Uri imageUri = resultData.getData();
}
}
}
Get Image from the Gallery and Show in ImageView
you can try this.
paste this code in your button click event.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
and below code is your on activity result
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
I hope it is helpful for you.
Dialog to pick image from gallery or from camera
The code below can be used for taking a photo and for picking a photo. Just show a dialog with two options and upon selection, use the appropriate code.
To take picture from camera:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code (called requestCode)
To pick photo from gallery:
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
onActivityResult
code:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
imageview.setImageURI(selectedImage);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
imageview.setImageURI(selectedImage);
}
break;
}
}
Finally add this permission in the manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Select multiple images from android gallery
The EXTRA_ALLOW_MULTIPLE option is set on the intent through the Intent.putExtra() method:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Your code above should look like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
Note: the EXTRA_ALLOW_MULTIPLE
option is only available in Android API 18 and higher.
Related Topics
How to Programmatically Tell If a Bluetooth Device Is Connected
Android and Facebook Share Intent
How to Add Manifest Permission to an Application
Check Android Application Is in Foreground or Not
How to Set My App as the Default Sms App
Disable Scrollview Programmatically
How to Control the Width and Height of the Default Alert Dialog in Android
Android Listview Selector Color
Proper Implementation of Viewpager2 in Android
How to Programmatically Add Views to Views
Difference Between Adjustresize and Adjustpan in Android
Change Volley Timeout Duration
How to Change the Background Color of Action Bar'S Option Menu in Android 4.2
Android: Broadcast Receiver for Screen on and Screen Off
Passing Image from One Activity Another Activity
Android - Set Max Length of Logcat Messages
How to Add Window Android.View.Viewroot$W@44Da9Bc0 -- Permission Denied for This Window Type