繁体   English   中英

如何在我的 LibGDX 游戏中分享屏幕截图?

[英]How to share a ScreenShot in my LibGDX game?

好的,正如我在标题中提到的,这就是我想要完成的。 我认为这是很容易实现的事情。

1.) 调用某种截图方法,将 .png 文件保存到某种路径。

2.) 获取路径并使用传递路径和 tada 开始一个意图,完成!

这是我到目前为止所拥有的,但我不确定这里有什么问题,当我点击共享时,“对话框菜单”会弹出,但是当我点击,例如,gmail,图像是空的? 任何帮助将不胜感激。 :D

我的安卓文件中的类:

public class ActionResolverAndroid implements ActionResolver {
    Context context;

    public ActionResolverAndroid(Context context) {
        this.context = context;
    }

    @Override
    public void shareIntent() {
        String filePath = Gdx.files.external("shareIMG.png").path();
        System.out.println(filePath);

        String text = "Look at my awesome picture";
        Uri pictureUri = Uri.parse(filePath);
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
        shareIntent.setType("image/*");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(shareIntent, "Share images..."));
    }
}

尝试并使其在核心工作:

buttonRate.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            button_clicked.play();
            game.screenShotFactory.saveScreenshot();
            game.actionResolver.shareIntent();
        }
    });

最后,屏幕截图类:

public class ScreenshotFactory {

    public static void saveScreenshot(){
        try
        {
            Gdx.files.external("shareIMG.png").delete();
            FileHandle fh;
            do
            {
//                fh = new FileHandle(Gdx.files.getLocalStoragePath() + "shareIMG.png");
                fh = Gdx.files.external("shareIMG.png");
                System.out.println(fh.path());
            }
            while (fh.exists());

            Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

            PixmapIO.writePNG(fh, pixmap);

            pixmap.dispose();
        }

        catch (Exception e)
        {
        }
    }

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown)
    {
        final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

        if (yDown)
        {
            // Flip the pixmap upside downx
            ByteBuffer pixels = pixmap.getPixels();
            int numBytes = w * h * 4;
            byte[] lines = new byte[numBytes];
            int numBytesPerLine = w * 4;
            for (int i = 0; i < h; i++) {
                pixels.position((h - i - 1) * numBytesPerLine);
                pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
            }
            pixels.clear();
            pixels.put(lines);
            pixels.clear();
        }
        System.out.println("SCREENSHOT TAKEN!!!");
        return pixmap;
    }
}

对于仍在尝试使其正常工作的其他任何人,它对我来说是使用什么

fh = new FileHandle(Gdx.files.getExternalStoragePath() + "shareIMG.png";

正如 Ashwani 建议的那样,而不是 ScreenshotFactory 中的其他文件句柄。

然后将 Android 代码稍微修改为如下所示:

String filePath = new FileHandle(Gdx.files.getExternalStoragePath() + "shareIMG.png").toString();
File file = new File(filePath);

String text = "Look at my awesome picture";
Uri pictureUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);

startActivity(Intent.createChooser(shareIntent, "Share images..."));

并且不要忘记外部存储的清单权限

在 AndroidManifest.xml 中检查适当的权限添加这个

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

我认为它会做..根据我的说法,你的代码的其余部分很好......

1) 可能你在 getScreenshot 中使用了错误的代码:

 pixels.clear(); pixels.put(lines); pixels.clear();

2)查看相同问题的解决方案: 如何使用“共享图像使用”共享意图在android中共享图像?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM