现在解决 程序如下所示:
1、采用 jni 转让 java 办法 启动专辑选择框
2、采用java得到的图片将被保存到本地
3、采用Cocos2d-x于 CCImage 阅读
JAVA码如下面:
//启动图片选择框
private void launchCamera()
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");//set intent type
intent.setAction(Intent.ACTION_GET_CONTENT);
//取得图片信息返回MainActivity
startActivityForResult(intent,1);
}
//图片选择回调
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(resultCode==RESULT_OK)
{
Uri uri = data.getData();
//通过URI获取图片绝对地址
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri,proj,null,null,null);
int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//游标跳到首位,防止越界
cursor.moveToFirst();
String img_path = cursor.getString(actual_image_column_index);
//通过地址获得位图信息
Bitmap bitmap =BitmapFactory.decodeFile(img_path);
saveMyBitmap("001", bitmap);
}
}
//保存图片到本地
private void saveMyBitmap(String bitName,Bitmap mBitmap)
{
File f = new File("/sdcard/" + bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
C++代码例如以下:
//读取本地存储数据
CCSprite* LoadingLayer::loadImage()
{
CCSprite* tempsprite = NULL;
const char* path = "/sdcard/001.png";
FILE* fp = fopen(path, "rb");
if (!fp)
{
return tempsprite;
}
fseek(fp,0,SEEK_END);
int len = ftell(fp);
fseek(fp,0,SEEK_SET);
char* buf = (char*)malloc(len);
fread(buf,len,1,fp);
fclose(fp);
if(len==0 || buf==NULL)
{
return tempsprite;
}
CCImage* img = new CCImage;
img->initWithImageData(buf,len);
free(buf);
cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D();
texture->initWithImage(img);
img->release();
tempsprite = CCSprite::createWithTexture(texture);
texture->release();
return tempsprite;
}
1、采用 jni 转让 java 办法 启动专辑选择框
2、采用java得到的图片将被保存到本地
3、采用Cocos2d-x于 CCImage 阅读
JAVA码如下面:
//启动图片选择框
private void launchCamera()
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");//set intent type
intent.setAction(Intent.ACTION_GET_CONTENT);
//取得图片信息返回MainActivity
startActivityForResult(intent,1);
}
//图片选择回调
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(resultCode==RESULT_OK)
{
Uri uri = data.getData();
//通过URI获取图片绝对地址
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri,proj,null,null,null);
int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//游标跳到首位,防止越界
cursor.moveToFirst();
String img_path = cursor.getString(actual_image_column_index);
//通过地址获得位图信息
Bitmap bitmap =BitmapFactory.decodeFile(img_path);
saveMyBitmap("001", bitmap);
}
}
//保存图片到本地
private void saveMyBitmap(String bitName,Bitmap mBitmap)
{
File f = new File("/sdcard/" + bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
C++代码例如以下:
//读取本地存储数据
CCSprite* LoadingLayer::loadImage()
{
CCSprite* tempsprite = NULL;
const char* path = "/sdcard/001.png";
FILE* fp = fopen(path, "rb");
if (!fp)
{
return tempsprite;
}
fseek(fp,0,SEEK_END);
int len = ftell(fp);
fseek(fp,0,SEEK_SET);
char* buf = (char*)malloc(len);
fread(buf,len,1,fp);
fclose(fp);
if(len==0 || buf==NULL)
{
return tempsprite;
}
CCImage* img = new CCImage;
img->initWithImageData(buf,len);
free(buf);
cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D();
texture->initWithImage(img);
img->release();
tempsprite = CCSprite::createWithTexture(texture);
texture->release();
return tempsprite;
}