How to open media browser directly to photo gallery? - android

I want to create an Android Gallery/Photos viewer that lets the user PICK a profile picture. Based on all the examples I looked at, this is my code for doing so.
val galleryIntent = Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
galleryIntent.type = "image/*"
galleryIntentLauncher.launch(galleryIntent)
Unfortunately this opens to the root of some file system, but I want it to open directly to the Gallery or Google Photos or whatever is the correct location for the photo gallery on the device.
I realize this varies by manufacturer (OnePlus, Samsung, Google, etc.) and that may be the reason what I want to do is impossible, but I know I've seen other apps do it.
Is there some formal/common way of doing this (i.e. opening directly to the photo gallery instead of the root directory)?

Related

Android - Get an image picked from a camera into a photo gallery

I am new to Android. This is my first question. I am testing on 4.4.4, KitKat.
I am using the Android Developers "Taking Photos Simply" code in my app.
(Taking Photos Simply Tutorial)
I want the photos taken by the user to be available to my app, but also available later in the gallery.
If I specify
File storageDir = getExternalFilesDir (null);
Then various cameras work (camera, opencamera, DSLR camera), but after I leave my app the images do not appear in the gallery.
If I specify
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Then the behavior is the same, except for one camera app: DSLR Camera. The photo does appear in that app's camera directory which is visible using the gallery later.
I want this behavior for my app. I want the photos that the user takes to appear in a directory that they can later see using the gallery app. I don't want to create duplicate images, however.
I also want to save modified images later on to the same directory.
Thanks for any help
but after I leave my app the images do not appear in the gallery.
It may eventually. Gallery apps usually use the MediaStore, and your file may eventually be added to it. Use MediaScannerConnection and scanFile() to expedite this process.
You need to request the media scanner to scan a file and add it to the media database.Use this code.
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
and see ACTION_MEDIA_SCANNER_SCAN_FILE in android developer site.Here mMediaUri is the Uri of your file.

Displaying pictures in Gallery without showing other Gallery functions

I want to display pictures in the Gallery but without giving the user the capability to manipulate/send them or change the settings of the Gallery application.
Currently I am using the following code:
Intent intent = new Intent(Intent.ACTION_VIEW,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
When I am using ACTION_PICK, the Gallery is opened in "clean" mode without all those functions (manipulate image, send image, change settings, etc) and this is what I am looking for but with ACTION_VIEW intent.
There are hundreds, if not thousands, of "gallery" apps, both pre-installed on Android devices and downloaded from places like the Play Store.
There are no options on ACTION_VIEW to allow you to request "manipulate/send them or change the settings". And there is no requirement that every gallery application honor such a request, even if there were a way to express it.
ACTION_VIEW, and similar implicit Intents, are for linking to third-party apps, where those third-party apps can do what they want. In that respect, it is like linking to a Web site -- you cannot control, from your site, what another site shows the user.
If you need absolute control over the UX here, write your own image viewer.

Different way to open a PDF than from current SD?

I know there are many similar questions, but I need specifics. I originally wanted my app to open PDFS within the app itself, but I have settled to send an intent activity to adobe reader. I am currently opening PDFS by looking for the file on the device itself. Is there a way I can have the PDFS in my app, and create a folder on the users device, and then look for them? or something similar? Obvisouly the user isn't going to have the PDF already installed on their device. Here is my current code.
Intent intent7 = new Intent();
intent7.setPackage("com.adobe.reader");
intent7.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/Auto-example.pdf")), "application/pdf");
startActivity(intent7);
Is there a way I can have the PDFS in my app, and create a folder on the users device, and then look for them?
You can put the PDF in internal storage (e.g., getFilesDir()), then use FileProvider to serve them via a ContentProvider. This sample project demonstrates serving a PDF from internal storage (copied there from assets/) and viewing it in the user's chosen PDF viewer. There is also an Android training module covering this.
With respect to the code that you have, please use ACTION_VIEW as your Intent action (e.g., pass that to the constructor) and delete the setPackage() line.

Picture is not showing in Gallery App in Android

Hello I am trying to create a picture and save it into one directory of the SD card programmatically but when I go to gallery then these picture doesn't show there. It shows when I open any SD card browser app.
Is there any way to force so that picture appear in Gallery App also at the same time when I am saving the picture in my directory.
Thanks in advance.
The system does not scan the disk content all the time, it would kill the performances.
When you add a picture that the Gallery needs to pick-up, use
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
This will allow the mediaScanner to have a look at the file immediately.
Note : if the file is incorrect for some reason, it will not have any effect.

Android ACTION_VIEW Multiple Images

I have between one and three photos I'd like my app to display. I won't know until runtime exactly how many photos are downloaded from the Internet.
I can't figure out how to create an Intent to display the photos. Right now I'm caching them on the sdcard under a folder I create by doing something like (sans error checking):
final File externalDirectory = Environment.getExternalStorageDirectory();
final String folder = externalDirectory.getAbsolutePath() + "/Android/data/" + packageName + "/files/";
This was explained in the Android Developer Reference.
I can get one photo to display by doing the following:
final Uri uri = Uri.fromFile(file);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
Util.startActivity(this, intent);
Where file is the file of a saved photo.
If it helps, I could save the images to any location available to my app, however I would prefer to not have the photos show up listed with the user's other personal photos as that may be annoying.
The Image Viewer has a menu option "Slideshow", so it must know about multiple photos.
I could create my own Image Viewer, but that seems like extra work and beyond what I would I reasonably expect. Even if I did this, I would like the user to be able to install a 3rd party Image Viewer and get a better experience with pan, zoom, share, ...
I tried using the directory of the cached photo files to create the Uri, but the Image Viewer shows a black page. If I pass in the file, it shows just that one file and no others.
I know this must be possible because I can see use the Gallery app and show the photos if I manually select the folder. Everytime I research this issue, the comments say it's not possible to show multiple images.
I suspect there's some magic incantation, but what?
I think your goal is out of your control. If the viewer app is designed to handle mutiple images or a directory, you may ask it to show as you want, but you are defined to the viewer's pattern.
I have installed a third-party image viewer called QuickPic. I just tested your code snippet and the system popped up a chooser dialog to let me select the app to show the images in the folder. If I select native gallery, what I see is just an empty folder, while the Quickpic works as I want.
PS: I tell my app the Uri of the folder this way:
intent.setDataAndType(Uri.fromFile(new File("//mnt/sdcard/test/")), MimeTypeMap.getSingleton().getMimeTypeFromExtension("png"));

Categories

Resources