Hi,
Please tell me how to saving data for Android and iOS !
Hi,
Please tell me how to saving data for Android and iOS !
Android code
public class AppActivity extends Cocos2dxActivity {
static AppActivity app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKWrapper.getInstance().init(this);
app = this;
initGallery();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
showGallery(requestCode, resultCode, data);
}
static int IMG_RESULT = 1;
String ImageDecode;
String[] FILE;
public void initGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG_RESULT);
}
public void showGallery(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == IMG_RESULT && resultCode == RESULT_OK && null != data) {
Uri URI = data.getData();
String[] FILE = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(URI, FILE, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(FILE[0]);
ImageDecode = cursor.getString(columnIndex);
cursor.close();
//String[] arr = ImageDecode.split("/");
//final String img = arr[arr.length-1];
final String img = "img.png";
Bitmap bitmap = BitmapFactory.decodeFile(ImageDecode);
//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
File file = new File(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + img);
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e) {}
app.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(app).create();
alertDialog.setTitle("title");
alertDialog.setMessage("message");
alertDialog.setIcon(R.drawable.star_big_off);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
app.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("var hello = require('HelloWorld'); hello.prototype.load(\""+img+"\");");
}
});
}
});
alertDialog.show();
}
});
}
} catch (Exception e) {
Toast.makeText(this, "Please try again", Toast.LENGTH_LONG).show();
}
}
}
JavaScript code :
var self;
cc.Class({
extends: cc.Component,
properties: {
label : cc.Label
},
// use this for initialization
onLoad: function () {
self = this;
},
load : function(img){
console.log(".......... : "+img);
cc.loader.load(jsb.fileUtils.getWritablePath()+ img, function (err, res) {
var sp = new cc.Node();
sp.parent = self.node;
sp.addComponent(cc.Sprite);
sp.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(res);
});
}
});