Save HTML5 Canvas Image to Gallery – Phonegap Android plugin

Update

Readers of this post have raised issues with the plugin. I have to personally check for the issues but while using the plugin if you find any issues, you might want to debug it in ADT. I will come up with a working plugin soon. Thanks.

Sometime back I worked on a Phonegap Android plugin that helps to save an HTML5 Canvas Image to the device’s gallery. Well, the title of this post may be misleading but note that when you save an image, you actually have to save it on the SD Card or the device’s memory. The Gallery is just an app that shows the collection of images from various locations on the SD Card. So, there’s nothing like saving an image directly to the Gallery.
I had this working with Phonegap 2.2.0 (the version that I used). Newer versions of Phonegap/Cordova are available and things might have changed a bit, specially in the way custom plugins are written. Hence, if you are using newer versions of Phonegap you should have a look at the official documentation before proceeding.
Alright, here is what I did. I tried to save an image using the FileWriter – Phonegap API, but as it turned out, it can save only text data on the device’s memory. So, only way to do it was write a Phonegap plugin, pass the Canvas details from JavaScript interface to the Java side and let the Java class save the Canvas image on the SD Card.
Note that I am not a Java developer, and there may be better ways of writing this plugin.

How to install the plugin?
You need to get hold of two files :
1) The Java class – SavePhotoPlugin.java
2) The JavaScript interface – SavePhotoPlugin.js

Read More »

Advertisements

Saving Image to Android device’s Gallery – Phonegap Android

Update
Save HTML5 Canvas Image to Gallery – Phonegap Android plugin
I have a Phonegap Android plugin that helps to save HTML5 Canvas Image to the Gallery. See it here.

Now, back to the actual post,

Quick snippet of code that will help a Phonegap Android app to save an image to the Android device’s SD Card and make it available to the Gallery app to show. The title of this post may be misleading but note that when you save an image, you actually have to save it on the SD Card or the device’s memory. The Gallery is just an app that shows the collection of images from various locations on the SD Card. So, there’s nothing like saving an image directly to the Gallery.

Moreover I tried to save an image using the FileWriter – Phonegap API, but as it turned out, it can save only text data on the device’s memory. So, only way to do it was write a Phonegap plugin, pass the image data from JavaScript interface to the Java side and let the Java class save the image on the SD Card. Well I am not going to talk on the entire plugin, but I will share the Java code,

The methods below should be included inside your plugin’s Java class. The idea is simple, whatever be your image/ image data (eg. Base64 data), you take that image data and save it on the SD Card using the FileOutputStream. You get the file path by querying the external storage public directory which is the first line inside the savePhoto() method below. Now, after the image is saved you broadcast it through the System’s Media Scanner so that it becomes available to the Android Gallery app and to other apps. Hope this is clear, time for action now.

private String savePhoto(Bitmap bmp)
{
  //File imageFileFolder = new File(Environment.getExternalStorageDirectory(),"MyFolder"); //when you need to save the image inside your own folder in the SD Card
  File path = Environment.getExternalStoragePublicDirectory(
     Environment.DIRECTORY_PICTURES
  ); //this is the default location inside SD Card - Pictures folder
  //imageFileFolder.mkdir(); //when you create your own folder, you use this line.
  FileOutputStream out = null;
  Calendar c = Calendar.getInstance();
  String date = fromInt(c.get(Calendar.MONTH))
  + fromInt(c.get(Calendar.DAY_OF_MONTH))
  + fromInt(c.get(Calendar.YEAR))
  + fromInt(c.get(Calendar.HOUR_OF_DAY))
  + fromInt(c.get(Calendar.MINUTE))
  + fromInt(c.get(Calendar.SECOND));
  File imageFileName = new File(path, date.toString() + ".jpg"); //imageFileFolder
  try
  {
    out = new FileOutputStream(imageFileName);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
    scanPhoto(imageFileName.toString());
    out = null;
  } catch (Exception e)
  {
    e.printStackTrace();
  }
  return imageFileName.toString();
}
private String fromInt(int val)
{
  return String.valueOf(val);
}

/* invoke the system's media scanner to add your photo to the Media Provider's database,
* making it available in the Android Gallery application and to other apps. */
private void scanPhoto(String imageFileName)
{
  Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  File f = new File(imageFileName);
  Uri contentUri = Uri.fromFile(f);
  mediaScanIntent.setData(contentUri);
  //this.cordova.getContext().sendBroadcast(mediaScanIntent); //this is deprecated
  this.cordova.getActivity().sendBroadcast(mediaScanIntent); 
}

Note that I am not a Java developer, and you may need to modify this code as per your needs. But it should work. First save your image (create a Phonegap Android app and test it) and after that you can see it being displayed in the gallery app. I did collect this code by browsing the net and made it work 100%. Otherwise it was difficult for me, since I did not find any concrete answer on this. Leave a suggestion if there is a better solution.

Fluidic Sliding Panels – Auto adjust to any device screen without browser refresh

Let’s talk about creating a sliding panel system that adjusts automatically to any screen size or rather any device resolution. The sliding is implemented using CSS3 Transitions and I have targeted only webkit based browsers. So the app will run on any iOS, Android devices including computer browsers such as Google Chrome and Apple Safari. If you have seen a Sencha Touch app which uses a nested list or the sliding animated panels then you must have noticed that the same app adjusts itself to any screen size as well as the portrait and landscape mode. So our demo will do the same. I have a very simple implementation of auto adjusting sliding panels.

In one of my previous tutorials I talked on the same Sliding Panel concept but the problem was that it did not auto adjust. If you check the demo on Google Chrome you will notice that only first time the app adjusts itself to the window width and height. Now resize Chrome on your computer and check– the app goes haywire isn’t it. Same thing will happen if you check the app in a mobile webkit browser and go from portrait to landscape mode or vice versa. The content you are looking is cut off. You have to refresh Chrome again to adjust the app. Now look at the new demo below,

New Demo link – http://jsphkhan.github.io/fluidic_sliding_panel/ (the demo is specifically meant for webkit based browsers)

Open it in the same Google Chrome browser and resize it. You will notice all the contents auto adjusts and even the sliding is now bounded to the new window size. So, by now you must have got an idea of what I am trying to communicate.

Read More »

Android or iPhone or Blackberry – How to detect using JavaScript

Sometimes when you are writing a mobile web app it becomes important to detect the device type in which the app will run in the browser. After finding out the device type you may write specific code for each device. One example can be handling different screen resolutions for the different device types and then make changes to the UI accordingly. Here’s a rundown of a small example that will show you how to detect the device type.

Mainly you need to detect the browser’s user agent and this will give you the device type. Following are the browser’s user agent for different devices:

For Android: Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1.

Currently, Android devices provide the following  in the User-Agent: “Android”, a version number, a device name, a specific build, Webkit version info, and “Mobile”. The above user agent string is for Android Froyo on a Nexus .

For iOS device (iPod/iPhone,iPad): Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5

This one is for the iPod that I am using. For iPhone and iPads the string format is similar, only it will be mentioned iPhone or iPad in place of iPod. The numbers may change depending on the format.

For Blackberry: Mozilla/5.0 (BlackBerry; U; BlackBerry AAAA; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/X.X.X.X Mobile Safari/534.11+

This one is for BlackBerry 6 and 7.

If you notice every user agent data has the device type name mentioned in it. That is what helps us to detect it. Let’s see how to do it.

Detecting user agent is very easy. Use the userAgent property of javascript’s navigator object. Below is the full java script code block,

var ua = navigator.userAgent;
var checker = {
iphone: ua.match(/(iPhone|iPod|iPad)/),
blackberry: ua.match(/BlackBerry/),
android: ua.match(/Android/)
};
if (checker.android) {
//code for Android
}
else if (checker.iphone) {
//code for iOS device
}
else if (checker. blackberry) {
//code for BlackBerry
}
else{}