在AppActivity.java文件中添加代码
public static String imgPath = "";
public static String imgName = "";
public static int aspectX = 1;
public static int aspectY = 1;
public static int outputX = 300;
public static int outputY = 300;
private static final int CODE_GALLERY_REQUEST = 0xa0;
private static final int CODE_CAMERA_REQUEST = 0xa1;
private static final int CODE_RESULT_REQUEST = 0xa2;
private static final String TAG = "Test";
public static AppActivity s_activity = null;
public static Context s_context = null;
在onCreate函数中添加
s_activity = this;
s_context = this.getApplicationContext();
修改onActivityResult函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK){
switch (requestCode) {
case CODE_GALLERY_REQUEST:
resizeImg(data.getData());
//resizePhoto(data.getData());
break;
case CODE_CAMERA_REQUEST:
String path = getExternalCacheDir().getPath();
String name = "camera_img.png";
Uri imageUri = null;
File camerafile = new File(path,name);
imageUri = Uri.fromFile(camerafile);
resizeImg(imageUri);
//resizeCamera();
break;
case CODE_RESULT_REQUEST:
s_activity.runOnGLThread(new Runnable() {
@Override
public void run() {
String js = "cc.NativeBridge.callSprite(";
String params = "'"+s_activity.imgPath+"'";
js = js + params + ");";
Cocos2dxJavascriptJavaBridge.evalString(js);
}
});
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
}
添加下面的代码
public static void pickPhoto(String name, int aspectX, int aspectY, int outputX, int outputY)
{
s_activity.imgName = name;
s_activity.aspectX = aspectX;
s_activity.aspectY = aspectY;
s_activity.outputX = outputX;
s_activity.outputY = outputY;
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
s_activity.startActivityForResult(intent, CODE_GALLERY_REQUEST);
}
public static void pickCamera(String name, int aspectX, int aspectY, int outputX, int outputY)
{
s_activity.imgName = name;
s_activity.aspectX = aspectX;
s_activity.aspectY = aspectY;
s_activity.outputX = outputX;
s_activity.outputY = outputY;
File outputfile = new File(s_activity.getExternalCacheDir(),"camera_img.png");
try {
if (outputfile.exists()){
outputfile.delete();
}
outputfile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Uri imageuri = Uri.fromFile(outputfile);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
s_activity.startActivityForResult(intent, CODE_CAMERA_REQUEST);
}
private void resizeImg(Uri uri)
{
try {
Intent intent = new Intent("com.android.camera.action.CROP");
File cropfile = new File(Environment.getExternalStorageDirectory().getPath(),s_activity.imgName);
if (cropfile.exists()){
cropfile.delete();
}
cropfile.createNewFile();
imgPath = cropfile.getPath();
Log.d(TAG, "##### "+cropfile.getPath());
Uri imageUri = uri;
Uri outputUri = null;
outputUri = Uri.fromFile(cropfile);
intent.putExtra("crop",true);
// aspectX,aspectY 是宽高的比例,这里设置正方形
intent.putExtra("aspectX",s_activity.aspectX);
intent.putExtra("aspectY",s_activity.aspectY);
//设置要裁剪的宽高
intent.putExtra("outputX", s_activity.outputX);
intent.putExtra("outputY",s_activity.outputY);
intent.putExtra("scale",true);
intent.putExtra("return-data",false);
if (imageUri != null) {
intent.setDataAndType(imageUri, "image/*");
}
if (outputUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
}
intent.putExtra("noFaceDetection", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(intent, CODE_RESULT_REQUEST);
} catch (IOException e) {
e.printStackTrace();
}
}