cocos android保存图片到相册,android平台 cocos2d-x 读取相册数据

现已解决 方案如下:

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值