好的,正如我在标题中所提到的那样,这就是我想要实现的目标。我认为这很容易实现。
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();
}
});
最后,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;
}
}
答案 0 :(得分:1)
检查AndroidManifest.xml中的相应权限 添加这个
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
并且我认为它会这样做......'根据我的说法,你的代码的其余部分很好......
答案 1 :(得分:1)
对于仍然试图让这个工作的其他人来说,它对我来说是用什么
fh = new FileHandle(Gdx.files.getExternalStoragePath() + "shareIMG.png";
而不是ScreenFactory中的其他文件句柄,正如Ashwani建议的那样。
然后稍微修改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..."));
不要忘记外部存储的清单权限
答案 2 :(得分:0)
1)可能你在行上的getScreenshot中使用了错误的代码:
pixels.clear(); pixels.put(lines); pixels.clear();
2)查看同一问题的解决方案:How to use "Share image using" sharing Intent to share images in android?