Sure, here is the code to take your picture from gallery in Android:
public void pickImageFromGallery() { // Check if the storage permission has been granted if (checkStoragePermission()) { // The permission has already been granted Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 100); } else { // Request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } }
if (requestCode == 100 && resultCode == RESULT_OK) {
// Get the image from the gallery
Uri imageUri = data.getData();
// Set the image in the image view
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(imageUri);
}
}
This code will first check if the WRITE_EXTERNAL_STORAGE permission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will start an activity to allow the user to select an image from the gallery.
The startActivityForResult() method has two parameters:
The intent that will be used to start the activity.
The request code that will be used to identify the result of the activity.
The activity that is started will return a result code. If the user selects an image from the gallery, the result code will be
RESULT_OK. The image that the user selected will be returned in the
data parameter of the onActivityResult() method.
In the onActivityResult() method, you can set the image in the image view by calling the
setImageURI() method of the image view.
To use this code, you would need to call the pickImageFromGallery() method in your app. For example, you could call the method in your app's
onCreate() method.
Here is an example of how you could call the pickImageFromGallery() method in your app's
onCreate() method:
Code snippet
public void onCreate() {
super.onCreate();
// Create an image view to display the image
imageView = findViewById(R.id.imageView);
// Set a listener to pick an image from the gallery
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickImageFromGallery();
}
});
}
When you run the app, you will see an image view. You can click on the image view to pick an image from the gallery.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the code to take your picture from gallery in Android:
public void pickImageFromGallery() { // Check if the storage permission has been granted if (checkStoragePermission()) { // The permission has already been granted Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 100); } else { // Request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
Code snippet
}
This code will first check if the
WRITE_EXTERNAL_STORAGEpermission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will start an activity to allow the user to select an image from the gallery.The
startActivityForResult()method has two parameters:The activity that is started will return a result code. If the user selects an image from the gallery, the result code will be
RESULT_OK. The image that the user selected will be returned in thedataparameter of theonActivityResult()method.In the
onActivityResult()method, you can set the image in the image view by calling thesetImageURI()method of the image view.To use this code, you would need to call the
pickImageFromGallery()method in your app. For example, you could call the method in your app'sonCreate()method.Here is an example of how you could call the
pickImageFromGallery()method in your app'sonCreate()method:Code snippet
When you run the app, you will see an image view. You can click on the image view to pick an image from the gallery.
/* Create object of ImageView */
ImageView editImage = (ImageView) findViewById(R.id.edit_image);
/* Take your picture from gallery */
editImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});