Adding Image To System Content Provider From Camera Intent - android

At the moment my app has a service running which is fired every hour. This pulls any photos thats have been taken since last time it was open and uploads then to the server. This is done using the system content provider
Now what I want to do is send an intent to open the camera app, I am doing this like so...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
What I would like to happen is when the focus comes back to my app (after the user has taken a picture), I can open the service and that takes care of the photo.
However it seems like the photo is never added to the content provider. The service opens it and the cursos has a count of 0. Is there anything i can do so the system adds the photo to the content provider or do I need to handle this myself?
Edit
So I figure there no intents I can use to get it to save to the system. So it would seem my options are either
contentResolver.insert()
or
MediaStore.Images.Mediea.insertImage()
What is the difference between these 2 methods?
Edit
So I am starting to think google are clueless with there implementation of this feature. Take this link
http://developer.android.com/guide/topics/media/camera.html#intent-receive
You pass in the Uri and when it returns to your app it gives you the uri you gave it. If you did not specify a uri it doesn't return one. What is the point in that? Why would I want data I already have? Surely it would make sense the other way around. Or even just giving you basic data you need such as name, path, mimetype. Would that be too difficult?

Notify MediaScanner, when the scan is complete your photo will be added to the system database.

Related

How To Bypass Folders Display And Go Straight To Photos Display By Using Android's INTENT Class?

I want my app to list/display only the phone's photos ( nothing else ) so that the user could select one of them whose bitmap will then be dealt later on by my remaining code.
The problem is that the code below instead of displaying photos directly, it first displays TWO FOLDERS ( Photos and Camera ) where photos are stored/grouped and waits for selection. How to bypass folders display/selection and go straight to photos display/selection ?
Here is the current code:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent,0);
I want my app to list/display only the phone's photos ( nothing else )
Query the MediaStore to find images and handle the UI yourself.
The problem is that the code below instead of displaying photos directly, it first displays TWO FOLDERS ( Photos and Camera )
It does that on your device. The results on other devices will vary, and there are ~2 billion of them, spread over ~20,000 device models. You are delegating to an indeterminate third-party app, which will vary by device and by user. It may result in a chooser for the user to select one of several apps that can handle your Intent.
If you do not like that behavior, query the MediaStore to find images and handle the UI yourself. This is more work, but you have complete control over the UI.

Catch error / exception from intent-called external application / activity

Let's say I have an application with an activity which list a bunch of files. When the user touch one of this files an ACTION_VIEW intent with proper mimeType and stuff is triggered so Android will prompt the user to choose one of the video players installed in the system (or the one that's defaulted).
I'm wondering if there is a way of telling if the playback of the file itself succeded or not. Because if I detect there was an error I can recomend the user other players available on Google Play.
¿Does the default Android Video Player responds the intent with some data? ¿Is there some generalized, common or standard way to get this information?
Any data on the subject is appreciated.
Thanks in advance.
I'm wondering if there is a way of telling if the playback of the file itself succeded or not.
You can tell if there was any eligible activity for the ACTION_VIEW Intent, either by catching ActivityNotFoundException on your startActivity() call, or by calling queryIntentActivities() ahead of time and checking for an empty list.
Once control passes to the third-party activity, that activity is like Las Vegas: what happens in that activity stays in that activity. You have no way of knowing anything about what went on, in terms of "success" or "failure" or anything else.

Android intent opposite of setComponent

I know I can force an intent to use a specific application/activity by using intent.setComponent(). But is there a way to say that it should not use a specific app/activity?
Why I need this:
My application will be able to respond to several urls (like http://www.companyname.com/something/12345). If my app launches cause of an Intent to this url, I'll check if I have the needed data to handle this. (In my example, something with id 12345). If I'm not able to do something useful with the intent, I create a new Intent with the same data, so the user can try to view the content in its browser.
This is the moment my problem occurs. When I start my newly created intent, my app appears again in the list of apps that can handle this. But I already know that my app can't do anything with this intent. The user already knows that my app can't handle the intent, but still it'd be nice if my app just didn't appear in this list.
Thanks in advance.
A possible solution that works is when creating the new intent, instead of intent.setData(uri) I do intent.setDataAndType(uri, "text/html").
This causes that my app disappears from the list. At the moment it's a solution that works :) But still I'd like to know if there are better ways to achieve this result.
You may use Intent-Filters for your app and set up the URI with wildcards as you need, see here for more information. With some additional info via an action or category, you may ensure, that your app it the only one (not) matching against the intent.

What does an Android Intent do with its URIs?

Reading through the document and a couple others, none really specified what an Intent does with the URI that it is passed. I'm simply curious as to what happens when it gets a URI. I know it depends on the scheme, so if the scheme was http, does it then attempt to open that web URI?
I ask since I'm trying to consume RESTful API that sends data back in JSON format. Having it open the URI for me when trying to pass the data from one Activity to another rather than making the HTTP call myself via HttpClient would be nice. Not a big deal but I was just curious if that is how works.
An Intent is just an object ... it does nothing with the URI. An Intent is used in function calls like startActivity(), sendBroadcast(), etc.
For example, when you pass an Intent to startActivity(), that Intent object is made available to the Activity. The Activity can interpret the URI as it pleases.
Android also uses Intent Filters to help route intents to various installed activities, these intent filters may inspect the URI.
Review http://developer.android.com/guide/components/intents-filters.html for all the info.
It's not entirely clear what you mean, however the behavior of Intents is basically dictated by the Android package manager. You can think of an Intent as being a procedure call: you specify someone to handle the Intent, along with some additional data (parameters), possibly some category, etc...
The Android package manager looks at your intent and basically asks the question "what app on the system is prepared to consume and handle this intent?" The system then opens up that app (if it is not already resident in memory), and then throws the intent at the app. Note that there are cases where there exist some possibility of ambiguity among intent handlers: multiple apps could be prepared to handle the intent. You can sometimes see this in the form of the user being asked to select what app should handle the intent (and the user can select a default one).
The dynamic semantics of how intents are handled depends, of course, on the set of apps installed on the system, and may change depending on the type of app installed. It sounds like, in your case, you are mostly concerned with intents that have an ACTION_VIEW action associated with them. In the general case, things that look like URLs will be "caught" by the browser (though there is no guarantee that this be the case!), and the package manager will look at the structure of the URI and say "hey, this looks like it should be handled by app X," I'm going to send this URL to it. (And, of course, new apps can change this behavior by registering other intent filters..)
An intent is an abstract description of an operation to be performed.uri specific data that intent has to do operation on it:
Intent Structure
The primary pieces of information in an intent are:
action: The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data: The data to operate on, such as a person record in the contacts database, expressed as a Uri.
Some examples of action/data pairs are:
ACTION_VIEW content://contacts/people/1 -- Display information about the person whose identifier is "1".
ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is
considered the most reasonable thing for a particular URI.
ACTION_VIEW content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical
top-level entry into the Contacts application, showing you the list of
people. Selecting a particular person to view would result in a new
intent { ACTION_VIEW content://contacts/N } being used to start an
activity to display that person.
So you can see that same Action with different data/uri perform different Action on data operate on.

notify installation complete from service

I have this piece of code
private void initiateInstallation() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/example.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
that from within my service installs an application named example.apk
I want after the installation is finished to run an activity which notifies the user about the installation.I did that except the activity appears before the installation finishes.
The problem is that within a service I cannot use startActivityForResult. So, I need a way around this so that I can start my notification activity(or for the sake of example just print something out with Toast within the service) only AFTER the installation is complete.
I already tried some answers from other questions like "alternative to startActivityforResult in services" but still I couldn't figure this out.
I also put the code so that maybe there may be something done in there.
Thanks in advance ... any suggestions are welcome.
You could listen to the PACKAGE_ADDED broadcast intent: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
As far as I know, these are sent after the installation is done, and you can listen to those from the service.
Just note that if the application was already installed, you will get ACTION_PACKAGE_CHANGED (as far as I know).
Also you must know the package name as well, not just the apk name, since the intent will contain the package name.
The answer given by #Pal Szasz is technically correct (as far as I know ;-) ).
However, based on the information given in your question, I assume you only wish to show a notification (no further programmatically actions are to be performed). If my assumptions are correct I would respectfully advise you NOT to show such a notification. And this is why:
The Android system already has a standard means of passing notifications to the user. The status bar will in this case already show you a message saying that the new app is successfully installed (or not installed in case of an error). If you implement yet another notification channel you will most likely confuse or irritate your users by diverging from the standard, expected behaviour.
Taking this beyond the borders of sanity one could also argue for the fact that you in some sense also would contribute to the fragmentation of Android (in a very small scale, but nevertheless).

Categories

Resources