cordova plugin for saving base64 image
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Base64SaveImage.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package org.solderzzc.Base64SaveImage;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.util.Calendar;
  5. import org.apache.cordova.CallbackContext;
  6. import org.apache.cordova.CordovaPlugin;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import android.content.Intent;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.os.Environment;
  15. import android.util.Base64;
  16. import android.util.Log;
  17. /**
  18. * Base64SaveImage.java
  19. *
  20. * Android implementation of the Base64SaveImage for iOS.
  21. * Inspirated by Joseph's "Save HTML5 Canvas Image to Gallery" plugin
  22. * http://jbkflex.wordpress.com/2013/06/19/save-html5-canvas-image-to-gallery-phonegap-android-plugin/
  23. *
  24. * @author Vegard Løkken <vegard@headspin.no>
  25. */
  26. public class Base64SaveImage extends CordovaPlugin {
  27. public static final String ACTION = "saveImageDataToLibrary";
  28. @Override
  29. public boolean execute(String action, JSONArray data,
  30. CallbackContext callbackContext) throws JSONException {
  31. if (action.equals(ACTION)) {
  32. String base64 = data.optString(0);
  33. if (base64.equals("")) // isEmpty() requires API level 9
  34. callbackContext.error("Missing base64 string");
  35. // Create the bitmap from the base64 string
  36. Log.d("Canvas2ImagePlugin", base64);
  37. byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
  38. Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  39. if (bmp == null) {
  40. callbackContext.error("The image could not be decoded");
  41. } else {
  42. // Save the image
  43. File imageFile = savePhoto(bmp);
  44. if (imageFile == null)
  45. callbackContext.error("Error while saving image");
  46. // Update image gallery
  47. scanPhoto(imageFile);
  48. callbackContext.success(imageFile.toString());
  49. }
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. private File savePhoto(Bitmap bmp) {
  56. File retVal = null;
  57. try {
  58. Calendar c = Calendar.getInstance();
  59. String date = "" + c.get(Calendar.DAY_OF_MONTH)
  60. + c.get(Calendar.MONTH)
  61. + c.get(Calendar.YEAR)
  62. + c.get(Calendar.HOUR_OF_DAY)
  63. + c.get(Calendar.MINUTE)
  64. + c.get(Calendar.SECOND);
  65. String deviceVersion = Build.VERSION.RELEASE;
  66. Log.i("Base64SaveImage", "Android version " + deviceVersion);
  67. int check = deviceVersion.compareTo("2.3.3");
  68. File folder;
  69. /*
  70. * File path = Environment.getExternalStoragePublicDirectory(
  71. * Environment.DIRECTORY_PICTURES ); //this throws error in Android
  72. * 2.2
  73. */
  74. if (check >= 1) {
  75. folder = Environment
  76. .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  77. if(!folder.exists()) {
  78. folder.mkdirs();
  79. }
  80. } else {
  81. folder = Environment.getExternalStorageDirectory();
  82. }
  83. File imageFile = new File(folder, "c2i_" + date.toString() + ".png");
  84. FileOutputStream out = new FileOutputStream(imageFile);
  85. bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
  86. out.flush();
  87. out.close();
  88. retVal = imageFile;
  89. } catch (Exception e) {
  90. Log.e("Base64SaveImage", "An exception occured while saving image: "
  91. + e.toString());
  92. }
  93. return retVal;
  94. }
  95. /* Invoke the system's media scanner to add your photo to the Media Provider's database,
  96. * making it available in the Android Gallery application and to other apps. */
  97. private void scanPhoto(File imageFile)
  98. {
  99. Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  100. Uri contentUri = Uri.fromFile(imageFile);
  101. mediaScanIntent.setData(contentUri);
  102. cordova.getActivity().sendBroadcast(mediaScanIntent);
  103. }
  104. }