现已解决 方案如下: 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; } |
android平台 cocos2d-x 读取相册数据
时间: 2024-10-07 00:00:34