-1

I have created a Button to capture screenshot of the Webview and save it to a Folder in DCIM. The Problem is when I click on the Button, the screenshot gets captured and saves in the Gallery but when I click again the Old Screenshot saves again with a New name.

Here is my Code. Please help

screenshot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = takeScreenshot();

            saveBitmap(bitmap);

        }
    });


 public Bitmap takeScreenshot() {
    mWebview.setDrawingCacheEnabled(true);
    return mWebview.getDrawingCache();

}


  public void saveBitmap(Bitmap bitmap) {

    Random r = new Random();
    long i1 = r.nextInt(9999-9) + 9;

    char[] chars = "ABCDEF".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 20; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }

    File directory = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/");
    if(!directory.exists()) {
        directory.mkdirs();
    }
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/NCERT"+i1+sb+".jpeg");
//    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imagePath)));

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

3 Answers3

0

According to your code, you are generating random numbers for each click and you are saving them with a filename that contains your random number right? Which is why you are getting multiple images with the different names...you might want to rethink the file name generation code to solve this issue..

You might want to remove this code :

Random r = new Random();
long i1 = r.nextInt(9999-9) + 9;

char[] chars = "ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 20; i++) {
    char c = chars[random.nextInt(chars.length)];
    sb.append(c);
}

and also remove the i1 variable from :

File imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/NCERT"+i1+sb+".jpeg");

to :

File imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/myScreenshot.jpeg");
ElamParithi Arul
  • 377
  • 1
  • 11
  • Sorry but you got it Wrong. I want the files with different name. The problem is the Old Screenshot gets saved again with new name rand I want New screenshot with new name – dhaval khanna Jul 24 '15 at 14:00
0

I think old bitmap still your drawing cache. Pls try to method in link.

https://stackoverflow.com/a/10793429/3307005

Community
  • 1
  • 1
user3307005
  • 216
  • 4
  • 12
-1

I got the Solution myself. Let me first Explain the problem I was facing. When I click on the button ,the screenshot gets saved and now suppose I scroll though the webview and click the Button again, My code saves the OLD SCREENSHOT with a new name rather than the new ScreenShot.

Solution:

public Bitmap takeScreenshot() {
    mWebview.clearfocus(); //Cleares the preivious focus
    mWebview.setDrawingCacheEnabled(true);
    return mWebview.getDrawingCache();

}

Thanks for helping out