how to launch photo gallery with search parameter in Android programatically - android

I am trying to programatically launch or open the photo gallery after passing a photo name search parameter.
I am new at this. I am thinking maybe intents can be used but not sure so I am wondering if anyone could please point me in the right direction. Preferably with some code examples.
Much thanks.
RE-EDIT
As requested, this is the code I have so far..
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// CAMERA_PIC_REQUEST = 2600;
cameraIntent = Intent.createChooser(intent,"Select Picture");

you can try follow code:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(photoUri);
context.startActivity(intent);

I think this is what you mean, correct me if I'm wrong:
String nameOfPhotoToBeRetrieved = "myPhoto";
String WHERE = "TITLE ="+nameOfPhotoToBeRetrieved;
Then you can use the following line to deliver a result to your cursor/listener:
CursorLoader(context,content_uri,null,WHERE,null,null).deliverResult(myCursor);

Related

How to pick only videos from gallery?

I want to be able to pick only videos from gallery but I only found tutorials that select images but I only want to be able to select videos.
Can anyone help?
For example, when you use an intent, you can set the type of your intent. Here I'm using "video/*" to get all videos of my device.
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("video/*");
startActivity(galleryIntent);
Try this code..
protected int REQUEST_TAKE_GALLERY_VIDEO = 3;
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);

Distinguish Intent coming from an IntentChooser

I'm having serious problems getting a picture coming from an IntentChooser. It lets you choose between getting a picture from the camera or your drive:
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
boolean cameraAvailable = isCameraAvailable(this);
String pickTitle = cameraAvailable? "Select or take a new Picture" : "Select a Picture";
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
if(cameraAvailable)
{
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
}
startActivityForResult(chooserIntent, GET_PICTURE);
Where isCameraAvailable(Context context) checks if there is a camera waiting in the system.
The problem is that, when in onActivityResult, both intents (take picture or get picture from gallery) have, of course, the same requestCode, so I have to make a difference in another way. I've found out that the intent coming from the camera, has an action of inline-data, while the other just has no action at all:
if(intent.getAction() == "inline-data")
{
bitmap = (Bitmap) intent.getExtras().get("data");
}
else
{
InputStream stream = getContentResolver().openInputStream(
intent.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
}
Now, the problem comes that I'm not pretty sure this is the best solution to differentiate, and I was thinking if there is a way to check the complete action (MediaStore.ACTION_IMAGE_CAPTURE or Intent.ACTION_GET_CONTENT) or another one. Thanks in advance.
Tiny edit: In my case, which consists in grabbing a picture from anywhere or create it from scratch, the example just works, but for the sake of improving my code (and finding a more generic answer) I'll leave it open to suggestions.

Android Intents.SHOW_OR_CREATE_CONTACT always show only the Uri string

I need to show/interact with a new contact before create it, and I need a simple way to add it to the phone contacts.
this is the code I use:
String contactPhone = "33333333";
Uri contactUri = Uri.parse(String.format("tel: %s", contactPhone));
Intent addContactIntent = new Intent(
ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, contactUri);
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME, "FirstName" );
addContactIntent.putExtra(ContactsContract.Intents.Insert.COMPANY,"CompanyName");
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE,contactPhone);
addContactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL,"contact#email.com");
startActivity(addContactIntent);
and this is the result. The problem is that the Intent show me only the Phone instead all the info added.
ContactsContract.Intents.Insert.ACTION if you want to interact before create it:
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION,ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(ContactsContract.Intents.Insert.NAME, getName());
intent.putExtra(ContactsContract.Intents.Insert.COMPANY, getCompany());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, getEmail());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, getPhone());
intent.putExtra(ContactsContract.Intents.Insert.POSTAL, getAddress());
startActivity(intent);
This works pretty well. I hope this will help you.
This site (http://wiresareobsolete.com/2011/10/simplifying-interaction-with-contacts/) shows and gives examples) that SHOW_OR_CREATE_CONTACT can only trigger off "tel" or "mailto"/email for searches.

How to make an intent with multiple actions

I want to display an activity chooser that shows all apps that can VIEW and/or EDIT some data. Is there an easy way to do this, or do I have to implement my own activity chooser dialog? Or maybe I can just subclass Intent? Thanks.
I found a partial solution by using EXTRA_INITIAL_INTENTS:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);
I say partial because if an app supports both ACTION_VIEW and ACTION_EDIT it will show up twice in the list, one of which will open the file for viewing and the other for editing, and you wouldn't necessarily know which is which. I think a complete solution would require a custom app chooser, as Tim suggested.
EDIT (Complete Solution!):
I found a solution that doesn't involving writing a custom app chooser. In order to differentiate ACTION_EDIT apps from ACTION_VIEW apps, I found a way to append a "(for editing)" string to the labels for one of them (in my case, ACTION_EDIT) by using the line of code Tim provided. In addition, to ensure the appended string doesn't appear to be a part of the app name, I changed the color of it to cyan:
PackageManager pm = kyoPrint.getPackageManager();
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent openInChooser = Intent.createChooser(viewIntent, "Open in...");
// Append " (for editing)" to applicable apps, otherwise they will show up twice identically
Spannable forEditing = new SpannableString(" (for editing)");
forEditing.setSpan(new ForegroundColorSpan(Color.CYAN), 0, forEditing.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
List<ResolveInfo> resInfo = pm.queryIntentActivities(editIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_EDIT);
intent.setDataAndType(uri, type);
CharSequence label = TextUtils.concat(ri.loadLabel(pm), forEditing);
extraIntents[i] = new LabeledIntent(intent, packageName, label, ri.icon);
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
EDIT 2: BUG
If there are no activities found by the first intent, NO activities will be displayed, including any found by the second intent. I ended up writing my own chooser. I just populated an ExpandableListView with headings for each type of intent with their respective activities as children (stored as individual LabeledIntents).
depends on what your data is. But in general using with ACTION_VIEW and some data attached you can use an IntentChoooser to populate the list of choices to the user.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some data");
startActivity(Intent.createChooser(intent, "Open with"));
Be sure to set your type correctly so that applications will know that you are wanting to open something that they may be able to handle.
EDIT: I think you would have to use a package manager query to get your two lists then combine them into one and make your own activity / dialog that will pop-up and get populated with the data contained in your combined list.
Here is an example making the query:
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
so if you make your two Intents and call this twice, passing in each intent you should be able to combine the resulting lists to get your full set of possibilities. Then it is up to to create an activity or dialog to show them with.

Any intents to show a single image with zooming/panning?

I have a png and a jpg image on disk. I'd like to use any built-in intent (if any are available) to view the image (just a single one at a time). Something like this:
Intent intent = new Intent();
intent.setImagePath("/blah/myimage.jpg");
startActivity(intent);
is there a built-in intent in android to do this, or do I have to write my own image viewing-activity? It would be cool if there was one that had panning/zooming as found in WebView,
Thanks
This should work.
Uri uri = Uri.fromFile("/blah/myimage.jpg");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);

Categories

Resources