好的,正如我在标题中提到的,这就是我想要完成的。我认为这是很容易实现的事情。
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/