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 Images Folder", "image Source: " + imgSrc + " , Image Name:" + imgName);
Context context= this.context;
AssetManager assetManager = context.getAssets();
File directory = new File("/sdcard/AppImages/");
directory.mkdirs();
File imageFile = new File(directory, imgName);
try {
InputStream inputStream = null;
inputStream = assetManager.open("www/" + imgSrc);
OutputStream outputStream = new FileOutputStream(imageFile);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
outputStream.write(data);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory())));
inputStream.close();
outputStream.close();
}
catch (IOException exception) {
Log.w("ExternalStorage", "Error writing " + imageFile, exception);
}
return imageFile.getAbsolutePath();
}
I have implemented the above code to save images to the device gallery. However, there seems to be a delay in displaying the image immediately after saving it. It only appears when I reload the application or the gallery after some time. Any suggestions on how to fix this issue would be greatly appreciated.