2017-04-01 92 views
0

我創建了一個遊戲,它有一個按鈕,點擊時會拍攝一個屏幕截圖。Android上的Libgdx錯誤當拍攝屏幕截圖時

ChangeListener changeListener = new ChangeListener() { 

        //take screenshot and share 
        if(actor.equals(shareBtn)){ 
         byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true); 
         Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888); 
         BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length); 
         PixmapIO.writePNG(Gdx.files.external("screenShot.png"), pixmap); 
         pixmap.dispose(); 
        } 

}; 

當我在PC上運行的遊戲沒有任何問題,但是當我在Android上運行它,我得到這個錯誤(我的手機擁有外置SD卡)。

com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: screenShot.png (External) 

Caused by: java.io.FileNotFoundException: /storage/emulated/0/screenShot.png: open failed: EACCES (Permission denied) 

回答

1

確保您已經添加權限寫入外部存儲在您的清單文件,如果你是針對Android SDK中< 23別的採取允許在運行時。

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

感謝它的工作,但你知道我怎樣才能分享屏幕截圖?因爲我教android會自動檢測圖像並給出選項分享。 –