2014-03-06 63 views
0
void SaveScreenshot() 
{ 
    CCSize size = CCDirector::sharedDirector()->getWinSize(); 
    CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);  
    texture->setPosition(ccp(size.width/2, size.height/2));  
    texture->begin(); 
    CCDirector::sharedDirector()->getRunningScene()->visit(); 
    texture->end(); 
    texture->saveToFile("screenshot.png", kCCImageFormatPNG); 
} 

如何在Android上使用原生Java访问屏幕截图?
我想为发送意图创建一个选择器,但我似乎无法找到cocos2dx saveToFile()写入..帮助的目录?保存截图cocos2d-x android

+0

你能说说你的问题吗? –

+0

设置断点,然后进入保存方法 – LearnCocos2D

+0

@SumitKandoi saveToFile()写入的目录是什么? – GameDevGuru

回答

3

你需要调用java方法来创建你的屏幕截图。

public static void ScreenShot() 
    { 
     Bitmap imageBitmap = BitmapFactory.decodeFile(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + "screenshot.png"); 

     String fileHolder = "SampleFolder"; 
     File filepathData = new File("/sdcard/" + fileHolder); 

     //~~~Create Dir 
     try { 
       if (!filepathData.exists()) 
       { 
        filepathData.mkdirs(); 
        filepathData.createNewFile(); 

        FileWriter fw = new FileWriter(filepathData + fileHolder); 
        BufferedWriter out = new BufferedWriter(fw); 
        String toSave = String.valueOf(0); 
        out.write(toSave); 
        out.close(); 
       } 
      } 
      catch (IOException e1) { 

      } 

      //~~~Create Image 
      File file = new File("/sdcard/" + "Your filename"); 

      try 
      { 
       file.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(file); 
       imageBitmap.compress(CompressFormat.PNG, 100, ostream); 
       ostream.close(); 
      } 
      catch (Exception e) {} 

      Uri phototUri = Uri.fromFile(file); 
      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
      //~~~Add Code Below 
    } 
+0

这看起来很有前途,但filepathData是一个只读文件系统'java.io.IOException:只读文件系统' – GameDevGuru

+0

@GameDevGuru sd卡wripe权限是否存在? –