0

Here is my code.I want to take screenshot when button is clicked and preview the image in iamgeview and store in sd card.But when the button is clicked nothing happens!!Iam new to androdi.plz help!thanks in advance

case R.id.screnshotid:
    Bitmap bitmap;
    View v1 = findViewById(R.id.Flayout);// get ur root view id
    v1.setDrawingCacheEnabled(true); 
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    try {
        fo.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    break;
jww
  • 97,681
  • 90
  • 411
  • 885
anusha
  • 55
  • 2
  • 10

1 Answers1

1
  Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

For saving

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
  File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg")
  f.createNewFile();
  FileOutputStream fo = new FileOutputStream(f);
  fo.write(bytes.toByteArray()); 
  fo.close();

Edit

ok.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            imageView.buildDrawingCache();
            Bitmap bm = imageView.getDrawingCache();

            OutputStream fOut = null;
            Uri outputFileUri;
            try {
                File root = new File(Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "folder_name" + File.separator);
                root.mkdirs();
                File sdImageMainDirectory = new File(root, "myPicName.jpg");
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);
            } catch (Exception e) {
            }
            try {
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
            }
        }
    });

Don't forget to add permissions or it wont work:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

By default it will save image inside your SDcard's folder and folder name is folder_name with test.jpg name.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84