1

I am working on an Android code where I get screenshot on button click. I could able to get the screenshot of the current activity first time but when I take the screenshot again it is not updating. I found that it takes the screenshot again but it saves with old image. This is my code :

    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.refreshDrawableState();
    v1.buildDrawingCache(true);
    Bitmap  myBitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.destroyDrawingCache();
    v1.setDrawingCacheEnabled(false);
    String filePath = Environment.getExternalStorageDirectory() + File.separator + "Pictures/myapp.png";
    File imagePath = new File(filePath);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        MediaScannerConnection.scanFile(MainActivity.this,
                new String[]{imagePath.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener()
                {
                    public void onScanCompleted(String path, Uri uri)
                    {
                        Log.w("ExternalStorage", "Scanned " + path + ":");
                        Log.w("ExternalStorage", "-> uri=" + uri);
                    }
                });
        send(imagePath);
    }
    catch (FileNotFoundException e)
    {
        Log.e("MSG", e.getMessage(), e);
    }
    catch (IOException e)
    {
        Log.e("MSG", e.getMessage(), e);
    }

Though this question is asked in different ways and have answers, I still couldn't solve the bug.

Reaching-Out
  • 415
  • 6
  • 26

2 Answers2

0

Maybe it's a file access problem ? You could try to check if your file already exists (even if you just created it like 500 ms before), sometimes android has very weird behaviours when it comes to rewriting files and opening them.

Either this or the view where you display your screenshot (if i undrestood correctly your problem) isn't "refreshed", and you could try to check there : How can you tell when a layout has been drawn?

Community
  • 1
  • 1
yan yankelevich
  • 885
  • 11
  • 25
  • I checked the access by including an if statement and it is running successfully. I'm just wondering, if the layout is not drawn then it should not have taken the screenshot at first time. – Reaching-Out Jun 24 '16 at 10:45
  • Well if i understand your problem correctly, the layout is drawn, what i'm trying to say it's that maybe just when you change the layout content it's not re-drawn. I had same kind of problem once (that's why i suggested the link above) and the trick with the view tree obeserver did solved it out. Maybe this won't help you at all, i'm just guessing what could cause this. PS : You should try to add imagePath.CreateNewFile(); after File imagePath = new File(filePath); – yan yankelevich Jun 24 '16 at 12:14
  • Yes I tried the link and I found that what you said is true. It is not re-drawn again. – Reaching-Out Jun 25 '16 at 03:42
0

Sometimes it happens.. I got the same problem in one of my project. I solved it by changing file name. if you change file name and access then it can be solved.

Adarsh
  • 270
  • 1
  • 3
  • 16
  • If I keep creating different name for the screenshots then I will end up increasing the gallery size. Also I tried renaming the file every time by appending the time but the same image is getting saved. – Reaching-Out Jun 24 '16 at 10:41
  • okey... i will check out different way.. and will get back to you if found anything – Adarsh Jun 24 '16 at 10:51