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

Khush
  • 867
  • 1
  • 19
  • 46

2 Answers2

0

you need to scan media manually, after your image has been saved, by following code:

 // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
    new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
    Log.i("ExternalStorage", "Scanned " + path + ":");
    Log.i("ExternalStorage", "-> uri=" + uri);
    }
    });
jeet
  • 29,001
  • 6
  • 52
  • 53
0

Try this:

After adding your image in gallery you need to broadcast

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+
 Environment.getExternalStorageDirectory())));

Hopefully it will work for you. :)

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Andy
  • 5,379
  • 7
  • 39
  • 53
  • I need to create a sendBroadcat mehtod for that. Could you kindly tell me what needs to be the content of the method and where should i place it? – Khush Jan 23 '12 at 08:16
  • After saving the image in gallery ,only need to paste this: Log.v("SAve To Gallery ", "saved file url: " + savedImgSrc); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+ Environment.getExternalStorageDirectory()))); thats it. no need to create anycbroadcast. – Andy Jan 23 '12 at 09:00
  • But it gives an error sayin "Sendbroadcast is not a function. create one." – Khush Jan 23 '12 at 09:28
  • check this: http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android – Andy Jan 23 '12 at 09:41