cocos2dx 将图像保存到 Android 画廊

2023-12-19

我想截屏并将其保存在图库中。 我尝试过的:

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) 纹理->saveToFile("scrshot.png", kCCImageFormatJPEG);或texture->saveToFile(“我的路径”); -> 它可以编译,但是图像保存在哪里?

2)使用JNI保存Bitmap文件->问题是如何将CCRenderTexture转换为Bitmap并解析它。


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

我的代码仅适用于iOS和Android

code is

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

在我的桥梁课程中

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本机方法

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);将调用本机方法,在本机方法中您可以保存图像路径并将其保存到图库

Note:getWritablePath() - 该函数将为您提供从 data->data->YOUR_PAKAGE 的路径

请随时提问。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cocos2dx 将图像保存到 Android 画廊 的相关文章

  • Android SoundPool 堆限制

    我正在使用 SoundPool 加载多个声音剪辑并播放它们 据我所知 它的功能 100 正确 但在 load 调用期间 我的日志中充斥着以下内容 06 09 11 30 26 110 ERROR AudioCache 23363 Heap
  • 如何更新 Firebase 中的节点密钥?

    如何重命名14 04 2017 node 没有用于重命名节点的 API 您必须获取节点的值 使用新名称将其保存到数据库并删除旧节点
  • Firebase Analytics 禁用受众国家/地区跟踪

    我正在开发一个严格不允许位置跟踪的应用程序 我想使用 Firebase Analytic 的其他功能 例如 PageTransitions 和 Crashalitics 但如果我无法禁用受众位置跟踪 我就无法使用其中任何功能 这是我在 An
  • Android:“dp”到“px”转换?

    我正在读这篇文章 http developer android com guide practices screens support html http developer android com guide practices scre
  • (Ionic 2)尝试回退到 Cordova-lib 执行时发生错误:TypeError:无法读取未定义的属性“then”

    Edit 使用 ionic 2 时会发生这种情况 我知道它还不稳定 但我认为可能有一些解决方案 因为其他人似乎没有遇到这个问题 Edit end 由于某种原因 我在尝试使用 ionic build android 和 ionic build
  • 接近语法错误(代码1)插入Android SQLite

    我正在创建一个通讯录应用程序 用户可以在其中输入姓名 电子邮件地址和号码 我希望将此数据保存在数据库中 但我似乎无法使插入方法起作用 我收到的错误是 android database sqlite SQLiteException near
  • 线程自动利用多个CPU核心?

    假设我的应用程序运行 2 个线程 例如渲染线程和游戏更新线程 如果它在具有多核 CPU 当今典型 的移动设备上运行 我是否可以期望线程在可能的情况下自动分配给不同的核心 我知道底层操作系统内核 Android linux内核 决定调度 我的
  • Android Studio 在编译时未检测到支持库

    由于 Android Studio 将成为 Android 开发的默认 IDE 因此我决定将现有项目迁移到 Android studio 中 项目结构似乎不同 我的项目中的文件夹层次结构如下 Complete Project gt idea
  • Firebase:如何在Android应用程序中设置默认通知渠道?

    如何设置default通知渠道通知消息当应用程序在后台运行时会出现什么情况 默认情况下 这些消息使用 杂项 通道 如你看到的在官方文档中 https firebase google com docs cloud messaging andr
  • CookieManager.getInstance().removeAllCookie();不删除所有cookie

    我在应用程序的 onCreate 中调用 CookieManager getInstance removeAllCookie 我遇到了一个奇怪的问题 我看到 GET 请求中传递了意外的 cookie 值 事实上 cookie 值是一个非常非
  • 在 Jetpack Compose 中启动动画矢量 Drawable

    我有一个动画矢量可绘制R drawable my anim 我想在 Jetpack Compose 中展示并开始 可绘制对象显示 渲染正确 但动画未启动 这是撰写视图 Composable fun SplashView Surface mo
  • Android 启动器快捷方式

    我制作了一个简单的打卡 打卡时钟应用程序 我想向用户添加在主屏幕上创建快捷方式的选项 该快捷方式将切换应用程序的状态 超时 超时 但我根本不希望此快捷方式在屏幕上打开应用程序 这是我的 setupShortcut private void
  • 如何在C(Linux)中的while循环中准确地睡眠?

    在 C 代码 Linux 操作系统 中 我需要在 while 循环内准确地休眠 比如说 10000 微秒 1000 次 我尝试过usleep nanosleep select pselect和其他一些方法 但没有成功 一旦大约 50 次 它
  • Android Webview 图像未加载

    我制作了一个简单的应用程序WebView 但有些图片无法加载 正确 在我的电脑上 错误 在模拟器中 Correct 错误 没有横幅 于是我用Chrome debug进行调试 发现我的代码被改变了 我不添加像noscript or style
  • 通过 ADB 拔出设备:“找不到服务”

    我必须测试我的应用程序在打瞌睡模式下的行为 根据文档 https developer android com training monitoring device state doze standby html testing doze 我
  • Android中webview的截图方法

    我在 webview 中的 html5 canvas 上画了一些线 并尝试使用下面的代码截取 webview 的屏幕截图 WebView webView WebView findViewById R id webview webView s
  • 用于推送通知的设备令牌

    我正在实施推送通知服务 我需要创建一个数据库来存储 4 个移动平台的所有设备令牌 我想根据他们的平台 iOS Android BlackBerry WP7 来组织它们 但是有什么方法可以区分平台 这样如果我只想向 Android 用户发送消
  • 将签名位图转换为签名字符串(很奇怪的一个)

    基本上我需要将位图图像转换为字符串 但这不是常见的 困境在于该字符串由两部分组成 1 积分 2 线路 我需要将图像转换为由 分隔的两个部分 我得到的一个例子是 221A 221A270A270A25032503200720071716171
  • 无法运行我的应用程序,要求选择 Android SDK

    今天我已经安装了Android Studio 金丝雀 1 现在我无法运行我的应用程序 将出现以下对话框 我已经通过 文件 gt 项目结构 gt Android SDK 位置 设置了正确的 SDK 位置 期待您的帮助来解决这个问题 警告对话框
  • 如何删除因 Google Fitness API 7.5.0 添加的权限

    将我的 play services fitness api 从 7 0 0 更新到 7 5 0 后 我注意到当我将新版本上传到 PlayStore 时 它 告诉我正在添加一个新权限和 2 个新功能 我没有这样做 有没有搞错 在做了一些研究来

随机推荐