Sharing photo on Instagram on Android using Titanium

Posted By : Akshay Luthra | 17-Nov-2015

Photo Sharing Instagram using Titanium

This blog is written to provide a ready code in Titanium for sharing photo on Instagram app.

 

You need to have the Instagram app installed on your Android device to share a photo in it.

 

The code written in this blog will download the image from a photoURL (say, sent from your Server), will save the image on your device and then will share the image on native Instagram app (only if the Instagram app is installed on your device).

 

if (!Ti.Network.online) {
	alert("No Internet Connection");
	return;
} else {

	var dir = (!Ti.Android || !Ti.Filesystem.isExternalStoragePresent()) ? Ti.Filesystem.applicationDataDirectory : Ti.Filesystem.externalStorageDirectory;
	var localStorage = Ti.Filesystem.getFile(dir, "IMAGES_FOLDER");
	(!localStorage.exists()) && localStorage.createDirectory();
	var imageFilePath = Ti.Filesystem.getFile(localStorage.resolve(), 'image.png');

	var xhr = Ti.Network.createHTTPClient({
		onload : function(e) {

			if (this.status == 200) {
				imageFilePath.write(this.responseData);

				if (Ti.Android) {

					var igIntent = Ti.Android.createIntent({
						action : Ti.Android.ACTION_SEND,
						packageName : "com.instagram.android",
						type : "image/*"
					});
					igIntent.putExtraUri(Titanium.Android.EXTRA_STREAM, imageFilePath.nativePath);
					setTimeout(function() {
						try {
							Ti.Android.currentActivity.startActivity(igIntent);
						} catch (e) {
							alert("Instagram app is not installed in your device!");
						}
					}, 400);

				}
			} else {
				alert("Error occurred while sharing on Instagram");
			}
		},
		onerror : function(e) {
			alert("Unable to proceed due to slow Internet Connection");
		}
	});

	xhr.setTimeout(10 * 1000);
	xhr.open('GET', photoURL);
	xhr.send();

}

 

 

THANKS

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..