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

标签 java android android-intent libgdx

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

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

2.) 获取路径并使用传递路径启动 Intent ,完成!

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

我的 ANDROID 文件中的类:

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

不要忘记外部存储的 list 权限

关于java - 如何在我的 LibGDX 游戏中分享屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33471328/

相关文章:

java - 如何从 HttpServletRequest 创建 okhttp3 请求?

java - System.currentTimeMillis()/1000 如何在 JAVA 中工作

java - 如何在大型 Java 代码库中找到所有基于原始 ("+"的字符串连接?

android - 如何在横向模式下启动启动画面?

c# - 在 Xamarin.Android 上以编程方式绘制 DrawableLeft?

Android 通知操作 - Intent Extra 未按预期工作

java - 自动装箱和性能

android - 如何从android中的按钮点击从一个 fragment 转到另一个 fragment ?

在应用营销功能中使用 Android 导致 Android 应用崩溃

android - 如何将数据从 Activity 发送到服务