Android Cocos2d-x flipping through album data

Source: Internet
Author: User

Now the resolver looks like this:

1, using the JNI transfer Java method to start the album selection box
2. Images from Java will be saved to the local
3, using Cocos2d-x in Ccimage reading

The Java code is as follows:

Start Picture selection box
private void Launchcamera ()
{
TODO auto-generated Method Stub
Intent Intent = new Intent ();
Intent.settype ("image/*");//set Intent Type
Intent.setaction (intent.action_get_content);

Get picture information back to Mainactivity
Startactivityforresult (intent,1);
}

Picture Selection Callback
protected void Onactivityresult (int requestcode,int resultcode,intent data)
{
if (RESULTCODE==RESULT_OK)
{
Uri uri = Data.getdata ();

Get image absolute address by 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 jumps to the first place to prevent cross-border
Cursor.movetofirst ();

String Img_path = cursor.getstring (Actual_image_column_index);

Get bitmap information by address
Bitmap Bitmap =bitmapfactory.decodefile (Img_path);

Savemybitmap ("001", bitmap);

}

}

Save picture to Local
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, FOut);

try {

Fout.flush ();

} catch (IOException e) {

E.printstacktrace ();
}

try {

Fout.close ();

} catch (IOException e) {

E.printstacktrace ();
}

}



C + + code such as the following:

Read locally stored data
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 flipping through album data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.