用户头像设置

  1. 得到图片:方法一:本地选取图片,剪裁出需要那部分,方法二:打开摄像头照相,把照片选取需要部分

  2. 上传到服务器,更新本地头像:

    代码如下:



  3. public class AvaterMainActivity extends Activity {


  4.     private static int CAMERA_REQUEST_CODE = 1;

  5.     private static int GALLERY_REQUEST_CODE = 2;

  6.     private static int CROP_REQUEST_CODE = 3;


  7.     @Override

  8.     protected void onCreate(Bundle savedInstanceState) {

  9.         super.onCreate(savedInstanceState);

  10.         setContentView(R.layout.test_activity_main);


  11.         Button btn_camera = (Button)findViewById(R.id.btn_camera);

  12.         btn_camera.setOnClickListener(new View.OnClickListener() {

  13.             @Override

  14.             public void onClick(View v) {

  15.                 

  16.                

  17.             if (hasBackFacingCamera() || hasFrontFacingCamera() ) {

  18.             

  19.                     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

  20.                 startActivityForResult(intent, CAMERA_REQUEST_CODE);

  21.             }

  22.             

  23.             else {

  24.             

  25. ToastManager.getInstance(AvaterMainActivity.this).show("没有找到摄像头");


  26. }

  27.                 

  28.             }

  29.         });


  30.         Button btn_gallery = (Button)findViewById(R.id.btn_gallery);

  31.         btn_gallery.setOnClickListener(new View.OnClickListener() {

  32.             @Override

  33.             public void onClick(View v) {

  34.                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

  35.                 intent.setType("image/*");

  36.                 startActivityForResult(intent, GALLERY_REQUEST_CODE);

  37.             }

  38.         });

  39.     }


  40.     private Uri saveBitmap(Bitmap bm)

  41.     {

  42.         File tmpDir = new File(Environment.getExternalStorageDirectory() + "/com.zhangzhengfa.avater");

  43.         if(!tmpDir.exists())

  44.         {

  45.             tmpDir.mkdir();

  46.         }

  47.         File img = new File(tmpDir.getAbsolutePath() + "avater.png");

  48.         try {

  49.             FileOutputStream fos = new FileOutputStream(img);

  50.             bm.compress(Bitmap.CompressFormat.PNG, 85, fos);

  51.             fos.flush();

  52.             fos.close();

  53.             return Uri.fromFile(img);

  54.         } catch (FileNotFoundException e) {

  55.             e.printStackTrace();

  56.             return null;

  57.         } catch (IOException e) {

  58.             e.printStackTrace();

  59.             return null;

  60.         }

  61.     }


  62.     private Uri convertUri(Uri uri)

  63.     {

  64.         InputStream is = null;

  65.         try {

  66.             is = getContentResolver().openInputStream(uri);

  67.             Bitmap bitmap = BitmapFactory.decodeStream(is);

  68.             is.close();

  69.             return saveBitmap(bitmap);

  70.         } catch (FileNotFoundException e) {

  71.             e.printStackTrace();

  72.             return null;

  73.         } catch (IOException e) {

  74.             e.printStackTrace();

  75.             return null;

  76.         }

  77.     }

  78.     private void startImageZoom(Uri uri)

  79.     {

  80.         Intent intent = new Intent("com.android.camera.action.CROP");

  81.         intent.setDataAndType(uri, "image/*");

  82.         intent.putExtra("crop", "true");

  83.         intent.putExtra("aspectX", 1);

  84.         intent.putExtra("aspectY", 1);

  85.         intent.putExtra("outputX", 150);

  86.         intent.putExtra("outputY", 150);

  87.         intent.putExtra("return-data", true);

  88.         startActivityForResult(intent, CROP_REQUEST_CODE);

  89.     }


  90.     @Override

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

  92.         if(requestCode == CAMERA_REQUEST_CODE)

  93.         {

  94.             if(data == null)

  95.             {

  96.                 return;

  97.             }

  98.             else

  99.             {

  100.                 Bundle extras = data.getExtras();

  101.                 if(extras != null)

  102.                 {

  103.                     Bitmap bm = extras.getParcelable("data");

  104.                     Uri uri = saveBitmap(bm);

  105.                     startImageZoom(uri);

  106.                 }

  107.             }

  108.         }

  109.         else if(requestCode == GALLERY_REQUEST_CODE)

  110.         {

  111.             if(data == null)

  112.             {

  113.                 return;

  114.             }

  115.             Uri uri;

  116.             uri = data.getData();

  117.             Uri fileUri = convertUri(uri);

  118.             startImageZoom(fileUri);

  119.         }

  120.         else if(requestCode == CROP_REQUEST_CODE)

  121.         {

  122.             if(data == null)

  123.             {

  124.                 return;

  125.             }

  126.             Bundle extras = data.getExtras();

  127.             if(extras == null){

  128.                 return;

  129.             }

  130.             Bitmap bm = extras.getParcelable("data");

  131.             ImageView imageView = (ImageView)findViewById(R.id.imageView);

  132.             imageView.setImageBitmap(bm);

  133.           sendImage(bm);

  134.         }

  135.     }


  136. //    

  137. //    向服务器传入图像

  138.     private void sendImage(Bitmap bm)

  139.     {

  140.         ByteArrayOutputStream stream = new ByteArrayOutputStream();

  141.         bm.compress(Bitmap.CompressFormat.PNG, 60, stream);

  142.         byte[] bytes = stream.toByteArray();

  143.         String img = new String(Base64.encodeToString(bytes, Base64.DEFAULT));


  144.         AsyncHttpClient client = new AsyncHttpClient();

  145.         RequestParams params = new RequestParams();

  146.         params.add("img", img);

  147.         client.post("http://192.168.56.1/ImgUpload.php", params, new AsyncHttpResponseHandler() {

  148.             @Override

  149.             public void onSuccess(int i, Header[] headers, byte[] bytes) {

  150.                 Toast.makeText(MainActivity.this, "Upload Success!", Toast.LENGTH_LONG).show();


  151.             }


  152.             @Override

  153.             public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

  154.                 Toast.makeText(MainActivity.this, "Upload Fail!", Toast.LENGTH_LONG).show();

  155.             }

  156.         });

  157.     }



  158.     

  159.     @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") private  boolean checkCameraFacing( int facing) {

  160.         if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {

  161.             return false;

  162.         }

  163.         final int cameraCount = Camera.getNumberOfCameras();

  164.         CameraInfo info = new CameraInfo();

  165.         for (int i = 0; i < cameraCount; i++) {

  166.             Camera.getCameraInfo(i, info);

  167.             if (facing == info.facing) {

  168.                 return true;

  169.             }

  170.         }

  171.         return false;

  172.     }

  173.     public  boolean hasBackFacingCamera() {

  174.         final int CAMERA_FACING_BACK = 0;

  175.         return checkCameraFacing(CAMERA_FACING_BACK);

  176.     }

  177.     public  boolean hasFrontFacingCamera() {

  178.         final int CAMERA_FACING_BACK = 1;

  179.         return checkCameraFacing(CAMERA_FACING_BACK);

  180.     }

  181.     public static int getSdkVersion() {

  182.         return android.os.Build.VERSION.SDK_INT;

  183.     }


  184. }


相应xml文件如下:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <ImageView

        android:id="@+id/imageView"

        android:layout_width="300px"

        android:layout_height="300px"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />


    <Button

        android:id="@+id/btn_camera"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="摄像头" />


    <Button

        android:id="@+id/btn_gallery"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/btn_camera"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="41dp"

        android:text="图库" />


</RelativeLayout>



转载于:https://my.oschina.net/jesonzhang/blog/422123

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值