if (action.equals("saveToGallery")) {
JSONObject obj = args.getJSONObject(0);
String imageSource = obj.has("imageSrc") ? obj.getString("imageSrc") : null;
String imageName = obj.has("imageName") ? obj.getString("imageName") : null;
String savedImgSrc = saveImageToGallery(imageSource, imageName);
Log.v("SAve To Gallery ", "saved file url: " + savedImgSrc);
return new PluginResult(PluginResult.Status.OK);
}
return new PluginResult(PluginResult.Status.INVALID_ACTION);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
public String saveImageToGallery(String imgSrc, String imgName) {
Log.v("Save To Gallery ", "image SRc: " + imgSrc + " , image Name:"
+ imgName);
Context ctx = this.ctx;
AssetManager assmgr = ctx.getAssets();
File tempDir = new File("/sdcard/HarmonyDayApp/wallpapers/");
tempDir.mkdirs();
File file = new File(tempDir, imgName);
try {
InputStream is = null;
is = assmgr.open("www/" + imgSrc);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory())));
is.close();
os.close();
} catch (IOException ex) {
Log.w("ExternalStorage", "Error writing " + file, ex);
}
return file.getAbsolutePath();
}
This is the code I am using to save the image to the device gallery. However, after the image is saved, if i check the gallery imeediately, the image is not there. It comes when i reload the application, or the gallery after sometime. Any suggestion to this problem will be helpful.