starting another android application to open a photo? - java

I had an interview today for a junior android dev position and one of the questions I was asked was something like this:
You have an application that opens an email. The email has a photo attached to it. How could you give the user the choice of which application to open this photo with? I.e. give them a list of photo opening apps to choose from and then open that application with the photo. You don't know ahead of time what apps are available other than android's default photo viewer.
How the heck do you solve this?

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/foo.jpg");
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
In a nutshell, you are telling Android, in an abstract way that you'd like to view a photo. Apps that are able to view photos register their ability to do so in their manifest (they respond to ACTION_VIEW, for the given content type image/*).
If you wanted to view an HTML file,
intent.setDataAndType(Uri.parse("http://www.google.com", "text/html");
ACTION_VIEW is just one example. There's also an ACTION_EDIT, and others.

Related

Showing the video of the link shared on Whatsapp for Android Java or kotlin on Whatsapp

Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, videoLink);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
I can send the link containing the video to someone on WhatsApp, however, I want to show the image of the video to the user on WhatsApp.
Is there a way to do this in java or kotlin
as in the picture
I am not sure what you're trying to do exactly but there are two options I can think of.
Firstly, if you're looking for something like what you have in the screenshot then no coding is required.
This is usually handled by Facebook when their bot scrapes the meta tags from the page that is shared.
If you wanted to accelerate this process you can manually put the link that you want to share in Facebook's Sharing Debugger and scrape it manually so that Facebook takes note of it.
Once that is done you can share the link and it should display video information including the image, title & description.
However, if you want to share only the video image/thumbnail you can do that as follows:
grab the video ID from the URL using REGEX or by taking a substring of the URL that holds the ID.
Plug that ID in one of the following URLs to get a link to an image that you can share instead of the video.
https://i3.ytimg.com/vi/<insert-youtube-video-id-here>/default.jpg
https://i3.ytimg.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
https://i3.ytimg.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
https://i3.ytimg.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
https://i3.ytimg.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
Thanks to #asaph for his details answer on getting Youtube thumbnail here

Launch user's custom navigation app

I've seen some URIs to start navigation but i'm not convince with it.
I used this URI which starts browser or google maps:
Uri u = Uri.parse("google.navigation:q=" + Uri.encode(o.getLocalization().noZipString()));
Intent in = new Intent(Intent.ACTION_VIEW, u);
getActivity().startActivity(in);
And this which starts google navigation:
Uri u = Uri.parse("http://maps.google.com/maps?daddr=" + Uri.encode(o.getLocalization().noZipString()));
Intent in = new Intent(Intent.ACTION_VIEW, u);
getActivity().startActivity(in);
Both methods works but the question is - will user be able to choose custom navigation if present on his phone or google apps are the only ones that will be launched this way? I'm not really sure, those strings don't look too universal but I'm not able to test it for now. I've seen navigation vendor who is posting his own custom URI scheme on he's site but I'm not going to search for custom URI for every single navigation on the market...
It strongly depends on the application itself. If the application defines that it can handle implicit intents such as those, Android will provide those as an option in the choose app dialogue.
And, only Android M has the ability to show which links an application can handle in the app settings pane, as far as I know.
See this android developer article on linking to other applications.

Sign APK on Client Phone

This is my first question on StackOverflow, and my english is poor, so please :D. I do this just for fun. I want to change the icon per user, letting users change the icon of the app. I think if I want to do this, I must read the apk self, unzip it, change che drawable/icon.png and rebuild it, at last to sign.
I try some code, but failed. The import reason is that, package sun.securate.* is not in Android Java framework. I write some code to do this, but it failed-_-
Who can help me? I can share my already written code.
Help me,Please!
Well another work around will be to create shortcut icon for Home Screen which launches the same Activity as the launcher. With this your launcher icon in drawer will be the actual one but on the home screen you will have different icon. You can also explore the idea of creating shortcut for other apps if you know the package info.
Ref:
For package info here are the doc.
PackageManager pkgMgr = getPackageManager();
For creating short cut here is the snippet (this is just for illustration):
// Setting the intent class ActivityClassToLaunch you need to try PackageManager
Intent i = new Intent(activity, ActivityClass.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
ShortcutIconResource iconResource = null;
iconResource = ShortcutIconResource.fromContext(activity, R.drawable.new_app_icon);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
result.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
activity.sendBroadcast(result);
If you just want user to change the app icon, why dont you load the icon from the SD card. And the icon that you ship with your package will be there if no icon is specified by user.
Here is the snippet that can help you.
onCreate(...)
setContentView(...);
setFeatureDrawable(Window.FEATURE_LEFT_ICON,
<your_drawable>);
....
}
If this does not work, try calling setFeatureDrawable before setContentView
You cannot change the launcher icon of a signed-and-sealed APK, except through a software upgrade.

How do I programmatically write a shortcut to a specific page on the homescreen of the Android launcher?

I am currently working on an Android application whereupon I programmatically write a shortcut to the home screen. Something like this:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent());
Bitmap newbit;
newbit=bd.getBitmap();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
(it was actually a StackOverflow post that pointed me to this solution).
Anyway, when I run the app in the emulator, the shortcut is written to the fifth page from the left(on 2.3 home screen ). A subsequent run writes the shortcut to the 4th page from the left on the home screen.
My question is this: is there a way to write the shortcut to the center page or the first page (like passing indexes for the 2-D array that seems to hold the app shortcuts)? am I missing something in the API?
Thanks!
You cannot decide where the shortcut will go.

Debugging Intents

I asked a question previously about what shows up in the IntentChooser when I send an Intent with ACTION_SEND and MIME type "image/png". The problem is that some phones come with a default messaging app that is not showing up in the list, mine included (myTouch 4G) as well as a user that I speak with over email (using an HTC EVO). If I choose a Send or Share option from the built in gallery app or another application on the same image I'm saving and attempting to send directly from my app, Messages shows up in the list. From my app it does not. Other phones don't have this problem, so it's clearly a platform specific thing. But that doesn't mean I should just ignore the problem.
So, I go to troubleshooting the issue. I register one of the activities in my app to receive the the same type of intent, and then hit a breakpoint to analyze the Intent object being sent from the two different ways of sending it.
The problem is, the intent I'm sending and the intent being sent from Gallery or AndroZip (where Messages does show up in the chooser) seem to be the same. They both have the same action, same categories, same flags, same mime type. What else can I inspect on the Intent from Gallery or AndroZip to tell if there's some more information I can add to my Intent to get the default messaging app to show up in the chooser in cases where it is not?
The problem is specific to HTC Sense phones, and it arises because their Gallery and Messaging apps are different to the stock ones.
Specifically the Intent sent from Gallery to Messaging has the action android.intent.action.SEND_MSG which is different to android.intent.action.SEND. The Sense messaging app doesn't handle SEND, unlike the stock messaging app.
So the question becomes, how is the Sense Gallery app creating an activity chooser dialog which combines both SEND and SEND_MSG ?
I've done some research and got mostway there... the code below works, but the "Messages" entry in the dialog appears at the top rather than in alphabetical order as per Gallery. Doubtless some more research into intents would correct that, but at least this works:
// Create a chooser for things that can ACTION_SEND images
Intent intent = new Intent(Intent.ACTION_SEND);
Uri data = Uri.parse("content://media/external/images/media/98");
intent.putExtra(Intent.EXTRA_STREAM, data);
intent.setType("image/jpeg");
Intent chooser = Intent.createChooser(intent, "Blah");
// Add the stupid HTC-Sense-specific secondary intent
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.putExtra(Intent.EXTRA_STREAM, data);
htcIntent.setType("image/jpeg");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
// Show the chooser
startActivity(chooser);
First of all, +1 to Reuben, he is the genius, not me. But I had to modify his code a bit to get it to work. Basically I had to putExtra() on the htcIntent or the image never got stuck to the Intent.
Tested and validated on a Droid X and HTC Incredible (which had the same problem until now thanks to Reuben).
Uri uri = Uri.fromFile(new File(mFile));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.setType("image/png");
htcIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, "Send Method");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
startActivity(chooser);
Edit: I realize I'm putting the image on two Intents now, but I couldn't get it to work any other way.
Instead of debugging the intents, why not try to compare how your starting the chooser with how the gallery is doing it. It is open source after all, so instead of trying to guess at the issue with the result, you can debug from the cause.
https://android.googlesource.com/platform/packages/apps/Gallery3D

Categories

Resources