使用我的应用程序当前视图的 ShareActionProvider 共享屏幕截图

Share screenshot using ShareActionProvider of my apps current view

我查看了所有相关的类似内容post,但没有人回答我的问题。

我的程序设置了一个 ShareActionProvider 来截取当前视图的屏幕截图并与任何可以与 .png 图像交互的应用程序共享。

问题:分享的截图总是之前的截图,这意味着我必须点击操作栏中的分享按钮两次才能获取当前页面或片段视图的截图。我希望用户能够只按一次按钮。此外,是否可以在首次创建 activity 时不截屏,而是等到按下共享按钮。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    // Find the MenuItem that we know has the ShareActionProvider
    MenuItem shareItem = menu.findItem(R.id.menu_item_share);

    // Get its ShareActionProvider
    mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();

    setShareIntent(getDefaultScreenshotShareIntent());

    mShareActionProvider.setOnShareTargetSelectedListener(MainActivity.this);

    // Return true so Android will know we want to display the menu
    return true;
}

public boolean onShareTargetSelected (ShareActionProvider source, Intent intent) {

    setShareIntent(getDefaultScreenshotShareIntent());

    return false;
}

// Call to update the share intent
// Connect the dots: give the ShareActionProvider its Share Intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

private Uri saveScreenShotDirectoryLocation() {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "Some Title");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            values);

    return uri;
}

private void screenShotHandler(Uri uri) {
    Bitmap screenShot = takeScreenShot(MainActivity.this);

    OutputStream outstream;
    try {
        outstream = getContentResolver().openOutputStream(uri);
        screenShot.compress(Bitmap.CompressFormat.PNG, 100, outstream);
        outstream.flush();
        outstream.close();
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

private Intent getDefaultScreenshotShareIntent() {

    Uri uri = saveScreenShotDirectoryLocation();

    Intent intent = new Intent(Intent.ACTION_SEND);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    intent.setType("image/png");

    long currenttime = System.currentTimeMillis();

    intent.putExtra(Intent.EXTRA_SUBJECT, "Some Title" + currenttime);

    File path = Environment.getDataDirectory();
    long usablePartitionSpace = path.getUsableSpace();

    if (usablePartitionSpace >= SCREENSHOT_FILE_SIZE_IN_BYTES ) {
        screenShotHandler(uri);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
    } else {
        Toast.makeText(this, "Not enough freespace for screenshot", Toast.LENGTH_SHORT).show();
    }

    intent.putExtra(Intent.EXTRA_TEXT, "Some Title");

    File file = new File(uri.getPath());
    file.delete();

    return intent;
}

private static Bitmap takeScreenShot(Activity activity)
{
    View view = activity.getWindow().getDecorView();
    //View view = (View) MainActivity.viewPager.getChildAt(MainActivity.viewPager.getCurrentItem());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
    view.destroyDrawingCache();
    return b;
}

我编辑的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    // Find the MenuItem that we know has the ShareActionProvider
    MenuItem shareItem = menu.findItem(R.id.menu_item_share);

    // Get its ShareActionProvider
    mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();

    Uri uri = saveScreenShotDirectoryLocation();
    screenShotHandler(uri);

    mShareActionProvider.setOnShareTargetSelectedListener(MainActivity.this);

    // Return true so Android will know we want to display the menu
    return true;
}

public boolean onShareTargetSelected (ShareActionProvider source, Intent intent) {

    Uri uri = saveScreenShotDirectoryLocation();
    screenShotHandler(uri);
    setShareIntent(getDefaultScreenshotShareIntent());

    return false;
}

private void screenShotHandler(Uri uri) {

    File path = Environment.getDataDirectory();
    long usablePartitionSpace = path.getUsableSpace();

   // if (usablePartitionSpace >= SCREENSHOT_FILE_SIZE_IN_BYTES ) {

        Bitmap screenShot = takeScreenShot(MainActivity.this);

        OutputStream outstream;
        try {
            outstream = getContentResolver().openOutputStream(uri);
            screenShot.compress(Bitmap.CompressFormat.PNG, 100, outstream);
            outstream.flush();
            outstream.close();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
   // } else {
  //      Toast.makeText(this, "Not enough freespace for screenshot", Toast.LENGTH_SHORT).show();
  //  }
    setShareIntent(getDefaultScreenshotShareIntent());
}

 private Intent getDefaultScreenshotShareIntent() {

    Uri uri = saveScreenShotDirectoryLocation();

    Intent intent = new Intent(Intent.ACTION_SEND);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    intent.setType("image/png");

    long currenttime = System.currentTimeMillis();

    intent.putExtra(Intent.EXTRA_SUBJECT, "Some Title" + currenttime);

    File path = Environment.getDataDirectory();
    long usablePartitionSpace = path.getUsableSpace();

   // if (usablePartitionSpace >= SCREENSHOT_FILE_SIZE_IN_BYTES ) {
        intent.putExtra(Intent.EXTRA_STREAM, uri);
    //}

    intent.putExtra(Intent.EXTRA_TEXT, "Some Title");

    return intent;
}

答:那么 ShareActionProvider 可能不是正确的工具。有一个截取屏幕截图的操作栏项目,然后只需使用 Intent.createChooser() 和 startActivity() 让用户共享屏幕截图。

截取屏幕截图时调用 setShareIntent()(例如,screenShotHandler() 内和周围某处)。现在,根据之前的 Intent (onShareTargetSelected()).

,您在用户已经选择要做什么之后调用 setShareIntent()