Downloading images from the gallery in android - android

I am developing an app in which i need to import images from the default gallery to the storage in sd card that I use for the app. Is it possible?

You can use an Intent to have the user pick an image from the default gallery.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
Uri path = getTempUri("/mnt/sdcard/yourfolder");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, filenameInt);
This will save the image picked by the user to the path you specified. There are some more options like cropping and scaling:
photoPickerIntent.putExtra("crop", "true")
.putExtra("aspectX", 4)
.putExtra("aspectY", 3)
.putExtra("outputX", 800)
.putExtra("outputY", 600)
.putExtra("scale", true);
I don't know if this is exactly what you're aiming for though.

Related

Open image for sharing

I have Android Xamarin application with imageview
OnClik I lunch intent to preview image
File file = new File(photoPath);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.FromFile(file), "image/*");
intent.SetFlags(ActivityFlags.NoHistory);
StartActivity(intent);
from apps I choose 'Gallery'
when the image is opend there are no sharing options
when I open image from device 'Gallery' has sharing options
Image is saved to SD card before preview.
what am I doing wrong ?
solution that worked for me
Intent intent = new Intent();
intent.PutExtra(Intent.ActionView, photoPath);
intent.SetType("image/*");
StartActivity(Intent.CreateChooser(intent, "Select Picture"));
now I get my image opend in gallery and I can use sharing options too
Do it using Intent.ACTION_SEND and you'll get that sharing option.
Also try with Intent.ACTION_GET_CONTENT.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 1);

Open gallery except .gif images

I want to open device gallery except .gif images.
I tried the following code but it showing all images in gallery, let me help
Intent pickPhoto = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if(Build.VERSION.SDK_INT >21){
pickPhoto.setType("image/*");
pickPhoto.setAction(Intent.ACTION_GET_CONTENT);
}
startActivityForResult(pickPhoto, PICK_FROM_GALLERY);
To open the device gallery except for .gif images, you need to set the supported mimeTypes for the gallery intent i.e., image types you need to pick from the gallery like png, jpg, jpeg, etc.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimeTypes = {"image/png", "image/jpg", "image/jpeg"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_GALLERY);

android- open gallery and choose image and video

In my project I want to open a gallery on a button click and should be able to pick image or video to get path of them.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
From above code i am able to open gallery but in this case i am only able to choose image. So, please help me in choosing video also.
Thanks in advance.
On Android 6.0 and above using "video/* image/" or "image/ video/*" type doesn't work, it only recognizes the first filter you specify. I solved the problem using this code:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(photoPickerIntent, Constants.SELECT_PHOTO);
Although this will ask the user which app they want to use to select the image/video.
You can use the next snippet:
Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);
But I think that it only work on ICS or bigger
You need use the following as picking Intent
Intent photoLibraryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoLibraryIntent.setType("image/* video/*");
Below code solved my problem
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
2022 Android 9
I've tried everything available online, and accidently mixed 2 solutions that turned out to work.
This only gives photo library and google photos as options, and you can select photos AND videos.
libraryIntent.setType("video/*, image/*");
String[] mimetypes = {"image/*", "video/*"};
libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
Not enough rep to comment, but #YYamil's response works well.
Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);
If you're using the new registerForResultActivity, create a copy of ActivityResultContracts.GetMultipleContents() and put in createIntent:
#CallSuper
override fun createIntent(context: Context, input: String): Intent {
return Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType(input)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*")) // this does the trick
}
This is the best i known...... Try this for once....
final CharSequence[] options = {"Images", "Videos", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this);
builder.setTitle("Select From...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Images")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Videos")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
dialog.dismiss();
}
});
builder.show();
Change your Intent to this:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
When trying to get videos you need to state to mediaStore that video is in order and not images as you wrote.
Well android has put a lot of restriction on accessing the external content. I ended up using 3rd party library. This one is good.:
https://github.com/AnilFurkanOkcun/UWMediaPicker-Android
implementation 'com.github.AnilFurkanOkcun:UWMediaPicker-Android:1.3.0'
UwMediaPicker
.with(this) // Activity or Fragment
.setGalleryMode(UwMediaPicker.GalleryMode.ImageGallery) // GalleryMode: ImageGallery/VideoGallery/ImageAndVideoGallery, default is ImageGallery
.setGridColumnCount(4) // Grid column count, default is 3
.setMaxSelectableMediaCount(10) // Maximum selectable media count, default is null which means infinite
.setLightStatusBar(true) // Is llight status bar enable, default is true
.enableImageCompression(true) // Is image compression enable, default is false
.setCompressionMaxWidth(1280F) // Compressed image's max width px, default is 1280
.setCompressionMaxHeight(720F) // Compressed image's max height px, default is 720
.setCompressFormat(Bitmap.CompressFormat.JPEG) // Compressed image's format, default is JPEG
.setCompressionQuality(85) // Image compression quality, default is 85
.setCompressedFileDestinationPath(destinationPath) // Compressed image file's destination path, default is "${application.getExternalFilesDir(null).path}/Pictures"
.launch{selectedMediaList-> } // (::onMediaSelected) // Will be called when media is selected

How can I upload photos from Android's mobile gallery?

I want to upload photos from Android's mobile gallery from my app. How can I do that?
You should use the MediaStore to be able to get pictures from the gallery:
http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html
Edit:You can get the image with this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1);
And you get the bitmap with this in your onActivityResult :
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity()
.getContentResolver(), data.getData());

Imitate crop function of system Gallery

i want to Imitate crop function of system Gallery as the pic http://i.6.cn/cvbnm/88/4a/be/94d8630ca9a3261154f0acf2ebd9f39d.png ,i have found the souce code ,at Gallery\src\com\android\camera , but i cannot add the edit rectangular on one pic,i know the edit rectangular is finiehed in the HighlightView.jave file.can you tell me how to add a edit rectangular on the pic,which can scale and move like edit rectangular
1.Fire the intent
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.putExtra("crop", "true");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(IMAGE_PATH_TO_SAVE_CROPPED_IMAGE)));
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RETURN_CODE);
2.Then in onActivityResult
Bitmap bitmap = BitmapFactory.decodeFile(IMAGE_PATH_TO_SAVE_CROPPED_IMAGE);
// bitmap will contain the cropped image

Categories

Resources