2

I am beginner android developer and trying to implement one function for overflow menu. My menu code is like this

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id==android.R.id.home) {
        finish();
    }else if (id == R.id.image) {
        takeScreenshot(); // getting error like The method takeScreenshot(Context, View) in the type QuoteViewActivity is not applicable for the arguments ()
        return true;
    }

and function code is like

public static void takeScreenshot(Context context, View view) {

    String path = Environment.getExternalStorageDirectory().toString()
            + "/" + "test.png";  


    View v = view.findViewById(android.R.id.content).getRootView();
    v.setDrawingCacheEnabled(true);

    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    OutputStream out = null;
    File imageFile = new File(path);

    try {
        out = new FileOutputStream(imageFile);
        // choose JPEG format
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
    } catch (FileNotFoundException e) {
        // manage exception
    } catch (IOException e) {
        // manage exception
    } finally {

        try {   
            if (out != null) {
                out.close();
            }

        } catch (Exception exc) {
        }

    }

    // onPauseVideo();
    Intent share = new Intent(Intent.ACTION_SEND);

    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
    share.setType("image/png");
    ((Activity) context).startActivityForResult(
            Intent.createChooser(share, "Share Drawing"), 111);
}

How can I call it on menu click ? Sorry for stupid question for developer Thanks

Rajubhai Rathod
  • 501
  • 2
  • 5
  • 14

3 Answers3

0

The way you handle the click on a Menu Item is like this:

First, setup your Options Menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater mnuInflater = getSupportMenuInflater();
   mnuInflater.inflate(R.menu.your_menu_xml, menu);        
   return true;
}

Handle the clicks here:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
    case R.id.menu_settings:
       // EITHER CALL THE METHOD HERE OR DO THE FUNCTION DIRECTLY
       yourMethod();

       return true;

   default:
       return super.onOptionsItemSelected(item);
  }
}

And do the function in the yourMethod() here:

private void yourMethod() {
    // TODO Auto-generated method stub      
}
Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34
0

From what I see, it should be the following:

takeScreenshot(this, findViewById(R.id.your_root_layout));

However, you can call takeScreenshot(); if takeScreenshot is not static and inside your activity.

Joshua
  • 5,901
  • 2
  • 32
  • 52
  • Hi ! takeScreenshot is not declared in any layout as well takeScreenshot is static – Rajubhai Rathod Jun 20 '16 at 08:29
  • @RajubhaiRathod Assume you want to call this in `MainActivity`. Then `this` is the instance of `MainActivity` and `R.id.your_root_layout` is the root layout of the `MainActivity`. – Joshua Jun 20 '16 at 08:32
0
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

try {
    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    File imageFile = new File(mPath);

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

    openScreenshot(imageFile);
} catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
}

}

don't forget this

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34