从ios/c+传递图像+;jniandroid的代码
我想为用户提供一个功能来保存当前屏幕的截图。 我使用COCOS2DX V3.0和C++,并且第一次实现这个特性。 我在谷歌上搜索了一下,找到了这段代码 此代码适用于ios照片中的存储图像 我认为字节数组可以解决我的问题,但我不知道如何通过jni方法/字节数组将此图像传输到android 更多的代码片段从ios/c+传递图像+;jniandroid的代码,android,c++,java-native-interface,byte,cocos2d-x-3.0,Android,C++,Java Native Interface,Byte,Cocos2d X 3.0,我想为用户提供一个功能来保存当前屏幕的截图。 我使用COCOS2DX V3.0和C++,并且第一次实现这个特性。 我在谷歌上搜索了一下,找到了这段代码 此代码适用于ios照片中的存储图像 我认为字节数组可以解决我的问题,但我不知道如何通过jni方法/字节数组将此图像传输到android 更多的代码片段 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string str = CCFileUtils::sharedFileUtils()
#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"));
}
publicstaticvoidsaveimageandroidjni(最终布尔值可见)
{
ContextWrapper c=新的ContextWrapper(me);
字符串路径=c.getFilesDir().getPath()+“/imageNameToSave.png”;
System.out.println(“要检查的Paht——”+路径);
File imgFile=新文件(路径);
位图myBitmap=BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ArrayList URI=新的ArrayList();
add(Uri.parse(path));
输出流输出;
//查找SD卡路径
文件filepath=Environment.getExternalStorageDirectory();
//在SD卡中创建新文件夹
File dir=新文件(filepath.getAbsolutePath()
+“/您的文件夹名/”;
dir.mkdirs();
//为保存的图像创建名称
File File=新文件(dir,“imageNameToSave.png”);
试一试{
输出=新文件输出流(文件);
//从0%-100%压缩为png格式图像
压缩(Bitmap.CompressFormat.PNG,100,输出);
output.flush();
output.close();
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
意图=新意图();
Uri pngUri=Uri.fromFile(文件);
intent.setAction(intent.ACTION\u SEND);
intent.putExtra(intent.EXTRA_流,pngUri);
intent.setType(“图像/jpeg”);
me.startActivity(Intent.createChooser(Intent,“共享图像”);
}
注意
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="any text you want here"
path="Pictures/"/>
</paths>
getWritablePath();由于运行时权限问题,现在无法工作,我已经尝试在android应用程序启动时请求权限,但仍然无法工作,所以不要建议我这样做
我想将捕获的图像保存到android设备中,但我找不到方法
调试
android:requestLegacyExternalStorage="true"
返回null作为位图,我尝试了几种方法来解码位图,当我把它放在try块上时,它显示给我
我发现data/data/package中没有png,所以我说getWritable路径不适用于android
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"));
}
这是我与Cocos社区福音传道者的对话
对于Android端,任何值得注意的帮助:
我没有看到任何清单代码,但这是创建路径所必需的。将
标签置于
之前:
文件夹res>layout>xml下
添加“file_path.xml”
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="any text you want here"
path="Pictures/"/>
</paths>
对不起,我忘了在我的问题上添加这些东西,但我已经尝试过了,我的意思是使用文件提供程序
// Create the file where the photo will be stored
private File createPhotoFile() {
// Name of file
String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
// Location of storage
File storedDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photo = null;
try {
// Creates file in storedDir
photo = File.createTempFile(name, ".jpg", storedDir);
} catch (IOException e) {
e.printStackTrace();
}
return photo;
}