• Save
Saving Image to Android device’s Gallery – Phonegap Android
Upcoming SlideShare
Loading in...5
×
 

Saving Image to Android device’s Gallery – Phonegap Android

on

  • 5,878 views

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 ...

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.
For the full post download this file or visit the actual blog post link: http://jbkflex.wordpress.com/2012/12/23/saving-image-to-android-devices-gallery-phonegap-android/

Update
I have a plugin to save HTML5 Canvas Image to Android Gallery : http://www.slideshare.net/jsphkhan/save-html5-canvas-image-to-gallery-phonegap-android-plugin

Statistics

Views

Total Views
5,878
Views on SlideShare
5,878
Embed Views
0

Actions

Likes
0
Downloads
12
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Saving Image to Android device’s Gallery – Phonegap Android Saving Image to Android device’s Gallery – Phonegap Android Document Transcript

  • Saving Image to Android device’sGallery – Phonegap AndroidActual blog post link: http://jbkflex.wordpress.com/2012/12/23/saving-image-to-android-devices-gallery-phonegap-android/Quick snippet of code that will help a Phonegap Android app to save an image to the Android device’s SDCard and make it available to the Gallery app to show. The title of this post may be misleading but notethat when you save an image, you actually have to save it on the SD Card or the device’s memory. TheGallery 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 saveonly text data on the device’s memory. So, only way to do it was write a Phonegap plugin, pass the imagedata 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 beyour image/ image data (eg. Base64 data), you take that image data and save it on the SD Card using theFileOutputStream. You get the file path by querying the external storage public directory which is the firstline inside the savePhoto() method below. Now, after the image is saved you broadcast it through theSystem’s Media Scanner so that it becomes available to the Android Gallery app and to other apps. Hopethis is clear, time for action now.private String savePhoto(Bitmap bmp){ //File imageFileFolder = newFile(Environment.getExternalStorageDirectory(),"MyFolder"); //when you needto 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 thisline. 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 systems media scanner to add your photo to the MediaProviders 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 isdeprecated 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 itshould work. First save your image (create a Phonegap Android app and test it) and after that you cansee it being displayed in the gallery app. I did collect this code by browsing the net and made it work100%. Otherwise it was difficult for me, since I did not find any concrete answer on this. Leave asuggestion if there is a better solution.