cocos2dx将图像保存到Android画廊 [英] cocos2dx save image in to gallery android
问题描述
我想截取屏幕截图并将其保存在图库中. 我尝试过的:
I want to take screenshots and save it in the gallery. What I have tried:
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中:
in iOS it is working perfectly and am able to save image in to documents, but the problem is in android :
有两种可能性
1)texture-> saveToFile("scrshot.png",kCCImageFormatJPEG);或texture-> saveToFile("My path"); ->可以编译,但是图像保存在哪里?
1) texture->saveToFile("scrshot.png", kCCImageFormatJPEG); or texture->saveToFile("My path"); -> It compiles but where is the image saved?
2)使用JNI保存位图文件->问题是如何将CCRenderTexture转换为位图并进行解析.
2) Use JNI to save Bitmap file -> problem is how to cast CCRenderTexture to Bitmap and parse it.
推荐答案
我很快解决了这个问题,但忘记给出答案了,但现在给出答案,因为它可能会对某人有所帮助
I solve this issue quickly but forgot to give the answer but now giving because it may help someone
我的代码仅适用于iOS和android
My code is only for iOS and 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中
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];
}
对于iOS,它将保存到图库
For iOS it will save to gallery
对于android,有一个类androidJNI.cpp
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);
}
}
和用于将图像保存到数据/数据的android native方法
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"));
}
从数据/数据访问该图像时将无法直接访问,请将该图像保存到sd卡中,并从cocos2dx对其进行访问
when access this image from data/data will not accessible directly here save this image to sd card and the access it from cocos2dx
但是对于Android,必须使用getWritablePath函数获取可写路径并将此值解析为saveToFile函数,而SaveImageAdmobJNI(true);
将调用本机方法,在本机方法中,您可以保存获取图像路径并将其保存到图库
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
注意::getWritablePath()-此函数将为您提供data-> data-> YOUR_PAKAGE
Note: getWritablePath() - this function will give you the path from data->data->YOUR_PAKAGE
随时提出疑问.
这篇关于cocos2dx将图像保存到Android画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!