安卓图片上传技术总结

安卓图片上传

protected void onActivityResult(int requestCode, int resultCode, Intent data){

super.onActivityResult(requestCode, resultCode, data);
ContentResolver resolver = getContentResolver();
if (resultCode == RESULT_OK) {
//---------------------简单方法
try {
// 获得图片的uri
Uri originalUri = data.getData();
// 将图片内容解析成字节数组
mContent = readStream(resolver.openInputStream(Uri
.parse(originalUri.toString())));
// 将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
// //把得到的图片绑定在控件上显示
up1ImageView.setImageBitmap(myBitmap);
} catch (Exception e) {
// TODO: handle exception
}

}

public void onClick(View arg0) {
// TODO Auto-generated method stub
final CharSequence[] items =
{ "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(Appointment.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener()
{
public void onClick ( DialogInterface dialog , int item )
{
// 这里item是根据选择的方式,
// 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if (item == 1){
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, REQUEST_CAMERA);
} else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();

//即拍即显示
// showPicturePicker(Appointment.this,false);
}

原文地址:https://www.cnblogs.com/wellsoho/p/4650993.html