cocos2dx将图像保存到Android画廊

cocos2dx将图像保存到Android画廊,第1张

概述我想截取屏幕截图并将其保存在图库中.我试过的CCSizesize=CCDirector::sharedDirector()->getWinSize();CCRenderTexture*texture=CCRenderTexture::create((int)size.width,(int)size.height,kCCTexture2DPixelFormat_RGBA8888);texture->setPosition(ccp(size.widt

我想截取屏幕截图并将其保存在图库中.
我试过的

CCSize size = CCDirector::sharedDirector()->getWinSize();CCRenderTexture* texture = CCRenderTexture::create((int)size.wIDth, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);texture->setposition(ccp(size.wIDth/2, size.height/2));texture->begin();CCDirector::sharedDirector()->getRunningScene()->visit();texture->end();texture->savetofile("scrshot.png", kCCImageFormatJPEG);

在iOS中,它工作正常,能够将图像保存到文档中,但是问题出在androID中:

有两种可能性

1)texture-> savetofile(“ scrshot.png”,kCCImageFormatJPEG);或texture-> savetofile(“ My path”); ->它可以编译,但是图像保存在哪里?

2)使用JNI保存位图文件->问题是如何将CCRenderTexture转换为Bitmap并进行解析.

解决方法:

我很快解决了这个问题,但忘记给出答案了,但现在给出答案,因为它可能会帮助某人

我的代码仅适用于iOS和AndroID

代码是

CCSize size = CCDirector::sharedDirector()->getWinSize();CCRenderTexture* texture = CCRenderTexture::create((int)size.wIDth, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);texture->setposition(ccp(size.wIDth/2, size.height/2));texture->begin();CCDirector::sharedDirector()->getRunningScene()->visit();texture->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)    std::string str =  CCfileUtils::sharedfileUtils()->getWritablePath();    str.append("/imagenameToSave.png");    const char * c = str.c_str();    texture->savetofile(c);    SaveImageAndroIDJNI(true);#else        texture->savetofile("imagenameToSave.png", kCCImageFormatPNG);        BrIDgeClass::shared()->savetoAlbum();#endif

在我的BrIDgeClass

voID BrIDgeClass:: savetoAlbum(){    NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory, NSUserDomainMask, YES);    Nsstring *documentsDirectory = [paths objectAtIndex:0];    Nsstring *yourArtPath = [documentsDirectory stringByAppendingPathComponent:@"/imagenameToSave.png"];    UIImage *image = [UIImage imageWithContentsOffile:yourArtPath];    UIImageWritetoSavedPhotosAlbum(image, nil, nil, nil);    UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Alert !" message:@"Photo Saved To Photos." delegate:nil cancelbuttonTitle:@"OK" otherbuttonTitles:nil, nil];    [alert show];}

对于iOS,它将保存到图库

对于androID有一个类androIDJNI.cpp

voID SaveImageAndroIDJNI(bool visible){        JniMethodInfo t;        if (JniHelper::getStaticmethodInfo(t, "yourPackagename/Classname"                                           ,"SaveImageAndroIDJNI"                                           ,"(Z)V"))        {            t.env->CallStaticVoIDMethod(t.classID,t.methodID,visible);        }    }

和用于将图像保存到数据/数据的androID native方法

static voID SaveImageAndroIDJNI(final boolean visible){    Contextwrapper c = new Contextwrapper(me);    String path = c.getfilesDir().getPath() + "/imagenameToSave.png";    System.out.println("Paht to check --"+path);    file imgfile = new  file(path);    Bitmap myBitmap = BitmapFactory.decodefile(imgfile.getabsolutePath());    ArrayList<Uri> uris = new ArrayList<Uri>();    uris.add(Uri.parse(path));    OutputStream output; // Find the SD Card path    file filepath = Environment.getExternalStorageDirectory();    // Create a new folder in SD Card    file dir = new file(filepath.getabsolutePath()            + "/Your Folder name/");    dir.mkdirs();    // Create a name for the saved image    file file = new file(dir, "imagenameToSave.png");    try {        output = new fileOutputStream(file);        // Compress into png format image from 0% - 100%        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);        output.flush();        output.close();    }    catch (Exception e) {        // Todo auto-generated catch block        e.printstacktrace();    }    Intent intent = new Intent();    Uri pngUri = Uri.fromfile(file);    intent.setAction(Intent.ACTION_SEND);    intent.putExtra(Intent.EXTRA_STREAM, pngUri);    intent.setType("image/jpeg");        me.startActivity(Intent.createChooser(intent, "Share Image"));    }

当从数据/数据访问此图像时,将无法直接访问此处,请将此图像保存到sd卡中,并从cocos2dx进行访问

但对于AndroID,必须使用getWritablePath函数获取可写路径,并将此值解析为savetofile函数和SaveImageAdmobJNI(true);.将调用本机方法,在本机方法中,您可以保存获取图像路径并将其保存到图库

注意:getWritablePath()-此函数将为您提供data-> data-> YOUR_PAKAGE的路径

随时提出疑问.

总结

以上是内存溢出为你收集整理的cocos2dx将图像保存到Android画廊全部内容,希望文章能够帮你解决cocos2dx将图像保存到Android画廊所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1088570.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存