cocos2dx save image in to gallery android

淺唱寂寞╮ 提交于 2019-12-05 20:03:04

I solve this issue quickly but forgot to give the answer but now giving because it may help someone

My code is only for iOS and 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

In My 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];
}

For iOS it will save to gallery

For android there is a class 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);
        }
    }

And android native method for save image to data/data

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"));    
}

when access this image from data/data will not accessible directly here save this image to sd card and the access it from cocos2dx

but for android have to get writable path using getWritablePath function and parse this value to saveToFile function and the SaveImageAdmobJNI(true); will call native method ,in native method you can save take the image path and save it to gallery

Note: getWritablePath() - this function will give you the path from data->data->YOUR_PAKAGE

Feel free to ask queries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!