使用我的应用当前视图的ShareActionProvider分享屏幕截图

时间:2015-02-28 13:33:47

标签: android android-actionbar share screenshot shareactionprovider

我查看了所有相关的相似帖子,但没有人回答我的问题。

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

问题:共享屏幕截图始终是之前的屏幕截图,这意味着我必须单击操作栏中的共享按钮两次才能获得当前页面或片段视图的屏幕截图。我希望用户能够只按一次按钮。此外,是否可能在首次创建活动时没有截屏,但要等到按下共享按钮。

@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()让用户共享屏幕截图。

1 个答案:

答案 0 :(得分:0)

截取屏幕截图时调用setShareIntent()(例如,screenShotHandler()内外的某处)。现在,在用户根据之前的setShareIntent()Intent)选择了要执行的操作后,您正在调用onShareTargetSelected()