From cfed2326c7e77602fa6278ddf99203d9aaaf8df7 Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Wed, 15 Jan 2014 08:08:50 -0500 Subject: Broadcast an intent to mediascanner after executing screencap PS2: Fix for spaces in file names After doing screencap /sdcard/test.png in shell the screenshot is captured but the gallery is not updated. So we should broadcast the intent android.intent.action.MEDIA_SCANNER_SCAN_FILE for the image to show up in gallery. Change-Id: I8e384865082c717842d70d376d5828d74a2ad780 Signed-off-by: Umair Khan --- cmds/screencap/screencap.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'cmds/screencap') diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp index a57de01..2b365d8 100644 --- a/cmds/screencap/screencap.cpp +++ b/cmds/screencap/screencap.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include @@ -86,6 +88,21 @@ static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo, return NO_ERROR; } +static status_t notifyMediaScanner(const char* fileName) { + String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://"); + String8 fileUrl("\""); + fileUrl.append(fileName); + fileUrl.append("\""); + cmd.append(fileName); + cmd.append(" > /dev/null"); + int result = system(cmd.string()); + if (result < 0) { + fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); + return UNKNOWN_ERROR; + } + return NO_ERROR; +} + int main(int argc, char** argv) { ProcessState::self()->startThreadPool(); @@ -112,10 +129,11 @@ int main(int argc, char** argv) argv += optind; int fd = -1; + const char* fn; if (argc == 0) { fd = dup(STDOUT_FILENO); } else if (argc == 1) { - const char* fn = argv[0]; + fn = argv[0]; fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); if (fd == -1) { fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); @@ -183,6 +201,7 @@ int main(int argc, char** argv) SkData* streamData = stream.copyToData(); write(fd, streamData->data(), streamData->size()); streamData->unref(); + notifyMediaScanner(fn); } else { write(fd, &w, 4); write(fd, &h, 4); -- cgit v1.1