好了废话不多说,看代码模块讲解.
/**
* 对话框 选择相片或者拍照
* @param context
*/
public void showPicturePicker(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("图片来源");
builder.setNegativeButton("取消", null);
builder.setItems(new String[] { "拍照", "相册" },
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
switch (which) {
case 0:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
break;
case 1:
intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
intent.putExtra("outputX", w + 10);
intent.putExtra("outputY", h + 10);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", false);
startActivityForResult(intent, CHOOSE_PICTURE);
break;
}
}
});
builder.create().show();
}
这里是拍照对话框,调用系统自带,实际到项目中,大部分会采用自定义对话框,这里就不麻烦了,用系统的方便.
第一个是拍照:
Intent intent = null;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PICTURE);
imageUri 是为了得到原图,如果不这样写,那么你得到的就是相机的压缩图,大部分人不愿意这样做.所以这里就指定了路径.
第二个是选择相册:
intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
intent.putExtra("outputX", w + 10);
intent.putExtra("outputY", h + 10);
intent.putExtra("scale", true);
intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString()); intent.putExtra("noFaceDetection", false); startActivityForResult(intent, CHOOSE_PICTURE);
这些参数和上面有一些不一样,这是为了得到选择图片的指定大小.
剪切图片代码:
/**
* 剪切失败
*
* @param uri
* @param outputX
* @param outputY
* @param requestCode
*/
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}
通过图片的设定的宽高去调用系统裁剪得到剪切后的图片,然后返回onActivityResult进行处理.
通过图片指定的 uri获得Bitmap.
/**
* 获取图片
*
* @param uri
* @return
* @throws IOException
*/
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
FileOutputStream outputStream = null;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(uri));
outputStream = new FileOutputStream("/mnt/sdcard/test/usericon.png");
bitmap.compress(CompressFormat.PNG, 100, outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} finally {
try {
outputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mBitmap = bitmap;
return bitmap;
}
上传图片代码:
/**
* 上传图像
*/
private void uploadUserImage() {
Log.d("ppp", "setUserImage()-----");
final File file = new File("/mnt/sdcard/test/usericon.png");
if (file.exists()) {
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
boolean ff = postFile.postFile(file);
handle();
}
};
}
}
得到图片文件,调用下面这方法进行上次.我把 部分参数改为中文提示了.
这块是上传图片的方法,是用apache开源库做的.
/**
*
* @param f上传的图片文件
* @return
*/
public boolean postFile(File file) {
if (file == null) {
return false;
}
String url = "上传路径";
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
MultipartEntity mpEntity = new MultipartEntity();
long l = file.length();
Log.i("tag", l + "");
ContentBody cbFile = new FileBody(file, "image/png");
ContentBody cbuserid = new StringBody("用户 ID");
mpEntity.addPart("avatar", cbFile);// 上传文件
mpEntity.addPart("user_ticket", cbuserid);// 上传用户名
httpPost.setEntity(mpEntity);
response = client.execute(httpPost);
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String json = EntityUtils.toString(response.getEntity(),
"UTF-8");
JSONObject jObject = new JSONObject(json);
if (jObject != null) {
if (jObject.has("status")) {
int value = jObject.getInt("status");
if (value == 1) {
return true;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
好了,到此拍照上传选择图片剪切讲解结束了.有问题留言.
点我下载代码