【问题标题】:Save Level screenshot and upload to Google+ in LIBGDX在 LIBGDX 中保存关卡屏幕截图并上传到 Google+
【发布时间】:2015-06-04 11:08:59
【问题描述】:

在 FINISHED-LEVEL 模式下,我以 PNG 格式打印我的游戏屏幕并将其保存到文件 (saveScreenshotNamePath)。然后我想使用 shareGoogleImage 将该级别的图片上传到 Google+。我只用文本测试了共享功能,它可以工作,但我在图像共享部分遇到问题。

在出现的谷歌上传对话框中看不到打印屏幕,我只看到标题文字;显然图像没有附加。这可能是什么问题?

代码如下:

这会将打印屏幕保存到文件中:

public static String saveScreenshotNamePath(String name){
    try{
        FileHandle fh;
        do{
            fh = new FileHandle(name + ".png");
        }while (fh.exists());
        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
        return fh.file().getAbsolutePath();
    }catch (Exception e){
        return "";
    }        
}

这会启动 Intent:

@Override
public void shareGoogleImage(int no_level, String path) {
    try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
    Intent share = new PlusShare.Builder(this)
        .setText("See my latest Game Level " + Integer.toString(no_level) + " score")
        .setType("image/png")
        .setStream(Uri.parse(path)).getIntent();
    startActivityForResult(share, 0);
}

我也尝试了 MediaStore 变体,但结果相同:

@Override
public void shareGoogleImage(int no_level, String path) {
    File tmpFile = new File(path);
    String photoUri = "";
    try {
        photoUri = MediaStore.Images.Media.insertImage(getContentResolver(), tmpFile.getAbsolutePath(), null, null); 
    } catch (FileNotFoundException e1) { e1.printStackTrace(); }

    try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
    Intent share = new PlusShare.Builder(this)
        .setText("See my latest Game Level " + Integer.toString(no_level) + " score")
        .setType("image/png")
        .setStream(Uri.parse(photoUri))
        .getIntent();
    startActivityForResult(share, 0);
}

这是位于 AndroidLauncher.java(活动文件)内的 shareGoogleImage 函数的游戏屏幕中的游戏内(核心文件)触发器(Scene2D ImageButton 按下):

ShareGoogleButton.addListener( new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y){
    if(!share_pressed){ 
        share_pressed = true;
        ShareGoogleButton.clearActions();
        ShareGoogleButton.addAction(sequence(moveBy(0f, 12f, 0.1f), moveBy(0f, -12f, 0.2f), run(new Runnable(){
            public void run(){  
                String pathto_screencap = ScreenshotFactory.saveScreenshotNamePath("google_share_image");
                game.google_facebook_services.shareGoogleImage(level_no, pathto_screencap);
                share_pressed = false;
            }
        })));                   
        }
    }
});

【问题讨论】:

    标签: android-activity libgdx google-play-services scene2d


    【解决方案1】:

    这里至少存在两个问题:无限循环和绝对文件路径。由于您将它包装在一个包罗万象的 catch 块中,没有有用的日志记录,因此您不会收到任何日志消息来帮助您调试它。

    在第一次调用该方法后,这将是一个无限循环,因为该文件将存在(假设它已成功写入)。不知道你为什么会有这个。

    do{
        fh = new FileHandle(name + ".png");
    }while (fh.exists());
    

    无论如何,如果您希望它与 G+ 等其他应用程序共享,则需要将其保存到外部目录(不是绝对目录),因此将其替换为:

    fh = Gdx.files.external(name + ".png");
    

    您还需要清单中的 write-external-storage 权限:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多