【发布时间】:2019-10-06 19:53:20
【问题描述】:
我想为用户提供一个功能来保存当前屏幕的屏幕截图。 我正在使用 cocos2d-x v3.0 和 c++,并且是第一次实现此功能。 我做了一些谷歌搜索,找到了这段代码
此代码非常适合在 ios 照片中存储图像
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithUTF8String: filename] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
我认为字节数组可以解决我的问题,但我不知道如何通过 jni 方法/字节数组将此图像传输到 android
更多代码 sn-p
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
std::string str = CCFileUtils::sharedFileUtils()->getWritablePath();
str.append("/imageNameToSave.png");
const char * c = str.c_str();
tex->saveToFile(c);
this->scheduleOnce(schedule_selector(Dressup_screen_BG_button_view::Photo_Save_Gallery_Android),1.0);
#else
tex->saveToFile("imageNameToSave.png", kCCImageFormatPNG);
this->scheduleOnce(schedule_selector(Dressup_screen_BG_button_view::Photo_Save_Gallery),1.0);
#endif
// This is native method which will save photo
-( void )ThisPhotoClick{
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];}
安卓方法
public 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"));
}
注意
getWritablePath();由于运行时权限问题,现在无法正常工作,我已经尝试在 android 应用程序启动时请求许可但仍然无法正常工作,所以不建议我这样做
我想将捕获的图像保存在我的安卓设备中,但我找不到方法
My implementation is reference from this question but it's not working
调试
返回 null 作为位图我尝试了几种解码位图的方法,当我将它放在 try 块上时,它显示open failed: ENOENT (No such file or directory)
我观察到 data/data/package 中没有 png,所以我说 getWritable 路径不适用于 android
这是我与 Cocos 社区布道者的conversation
任何帮助都非常明显
【问题讨论】:
标签: android c++ java-native-interface byte cocos2d-x-3.0