问题
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);
in iOS it is working perfectly and am able to save image in to documents, but the problem is in android :
There are two possibilities
1) texture->saveToFile("scrshot.png", kCCImageFormatJPEG); or texture->saveToFile("My path"); -> It compiles but where is the image saved?
2) Use JNI to save Bitmap file -> problem is how to cast CCRenderTexture to Bitmap and parse it.
回答1:
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.
来源:https://stackoverflow.com/questions/24078695/cocos2dx-save-image-in-to-gallery-android