简体   繁体   English

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

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

Ok so as I have mentioned in the title, that is what I am trying to accomplish.好的,正如我在标题中提到的,这就是我想要完成的。 I would think that this is something very easy to implement.我认为这是很容易实现的事情。

1.) Invoke somesort of screenshot method that would save the .png file to somesort of path. 1.) 调用某种截图方法,将 .png 文件保存到某种路径。

2.) Get path and start an intent using the passing the path through and tada, done! 2.) 获取路径并使用传递路径和 tada 开始一个意图,完成!

Heres what I have so far but im just not sure whats wrong here, when I click on share the "dialog menu" pops out but when I click, for instance, gmail, the image is empty?这是我到目前为止所拥有的,但我不确定这里有什么问题,当我点击共享时,“对话框菜单”会弹出,但是当我点击,例如,gmail,图像是空的? Any help would be greatly appreciated.任何帮助将不胜感激。 :D :D

CLASS IN MY ANDROID FILES:我的安卓文件中的类:

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..."));
    }
}

ATTEMPT AND MAKING IT WORK IN CORE:尝试并使其在核心工作:

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

LASTLY, THE SCREENSHOT CLASS:最后,屏幕截图类:

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;
    }
}

For anyone else still trying to get this working, what did it for me was using对于仍在尝试使其正常工作的其他任何人,它对我来说是使用什么

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

Instead of the other file handle in the ScreenshotFactory, as Ashwani suggested.正如 Ashwani 建议的那样,而不是 ScreenshotFactory 中的其他文件句柄。

Then the Android code is slightly modified to look like this:然后将 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..."));

And don't forget the manifest permissions for external storage并且不要忘记外部存储的清单权限

Check for the appropriate permission in AndroidManifest.xml add this在 AndroidManifest.xml 中检查适当的权限添加这个

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

and i think it will do..'cauz rest of your code is fine according to me...我认为它会做..根据我的说法,你的代码的其余部分很好......

1) Possible you use wrong code in getScreenshot on lines: 1) 可能你在 getScreenshot 中使用了错误的代码:

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

2) Look solutions of the same problem: How to use "Share image using" sharing Intent to share images in android? 2)查看相同问题的解决方案: 如何使用“共享图像使用”共享意图在android中共享图像?

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

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