1<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 2<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 3<uses-permission android:name="android.permission.INTERNET"/>

 1 <RelativeLayout  2 xmlns:android="http://schemas.android.com/apk/res/android"  3  android:layout_width="match_parent"  4  android:layout_height="match_parent">  5  6 <ImageView  7 android:layout_width="300px"  8  android:layout_height="300px"  9  android:id="@+id/imageView" 10  android:layout_alignParentTop="true" 11  android:layout_centerHorizontal="true" /> 12 13 <Button 14 android:layout_width="wrap_content" 15  android:layout_height="wrap_content" 16  android:text="摄像头" 17  android:id="@+id/btn_camera" 18  android:layout_centerVertical="true" 19  android:layout_centerHorizontal="true" /> 20 21 <Button 22 android:layout_width="wrap_content" 23  android:layout_height="wrap_content" 24  android:text="图库" 25  android:id="@+id/btn_gallery" 26  android:layout_below="@+id/btn_camera" 27  android:layout_centerHorizontal="true" 28  android:layout_marginTop="41dp" /> 29 </RelativeLayout>

  1 public class MainActivity extends ActionBarActivity {  2  3 private static int CAMERA_REQUEST_CODE = 1;  4 private static int GALLERY_REQUEST_CODE = 2;  5 private static int CROP_REQUEST_CODE = 3;  6  7  @Override  8 protected void onCreate(Bundle savedInstanceState) {  9 super.onCreate(savedInstanceState);  10  setContentView(R.layout.activity_main);  11  12 Button btn_camera = (Button) findViewById(R.id.btn_camera);  13 btn_camera.setOnClickListener(new View.OnClickListener() {  14  @Override  15 public void onClick(View v) {  16 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  17  startActivityForResult(intent, CAMERA_REQUEST_CODE);  18  }  19  });  20  21 Button btn_gallery = (Button) findViewById(R.id.btn_gallery);  22 btn_gallery.setOnClickListener(new View.OnClickListener() {  23  @Override  24 public void onClick(View v) {  25 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  26 intent.setType("image/*");  27  startActivityForResult(intent, GALLERY_REQUEST_CODE);  28  }  29  });  30  }  31  32 private Uri saveBitmap(Bitmap bm) {  33 File tmpDir = new File(Environment.getExternalStorageDirectory() + "/com.dr.avater");  34 if (!tmpDir.exists()) {  35  tmpDir.mkdir();  36  }  37 File img = new File(tmpDir.getAbsolutePath() + "avater.png");  38 try {  39 FileOutputStream fos = new FileOutputStream(img);  40 bm.compress(Bitmap.CompressFormat.PNG, 85, fos);  41  fos.flush();  42  fos.close();  43 return Uri.fromFile(img);  44 } catch (FileNotFoundException e) {  45  e.printStackTrace();  46 return null;  47 } catch (IOException e) {  48  e.printStackTrace();  49 return null;  50  }  51  }  52  53 private Uri convertUri(Uri uri) {  54 InputStream is = null;  55 try {  56 is = getContentResolver().openInputStream(uri);  57 Bitmap bitmap = BitmapFactory.decodeStream(is);  58  is.close();  59 return saveBitmap(bitmap);  60 } catch (FileNotFoundException e) {  61  e.printStackTrace();  62 return null;  63 } catch (IOException e) {  64  e.printStackTrace();  65 return null;  66  }  67  }  68  69 private void startImageZoom(Uri uri) {  70 Intent intent = new Intent("com.android.camera.action.CROP");  71 intent.setDataAndType(uri, "image/*");  72 intent.putExtra("crop", "true");  73 intent.putExtra("aspectX", 1);  74 intent.putExtra("aspectY", 1);  75 intent.putExtra("outputX", 150);  76 intent.putExtra("outputY", 150);  77 intent.putExtra("return-data", true);  78  startActivityForResult(intent, CROP_REQUEST_CODE);  79  }  80  81  @Override  82 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  83 if (requestCode == CAMERA_REQUEST_CODE) {  84 if (data == null) {  85 return;  86 } else {  87 Bundle extras = data.getExtras();  88 if (extras != null) {  89 Bitmap bm = extras.getParcelable("data");  90 Uri uri = saveBitmap(bm);  91  startImageZoom(uri);  92  }  93  }  94 } else if (requestCode == GALLERY_REQUEST_CODE) {  95 if (data == null) {  96 return;  97  }  98  Uri uri;  99 uri = data.getData(); 100 Uri fileUri = convertUri(uri); 101  startImageZoom(fileUri); 102 } else if (requestCode == CROP_REQUEST_CODE) { 103 if (data == null) { 104 return; 105  } 106 Bundle extras = data.getExtras(); 107 if (extras == null) { 108 return; 109  } 110 Bitmap bm = extras.getParcelable("data"); 111 ImageView imageView = (ImageView) findViewById(R.id.imageView); 112  imageView.setImageBitmap(bm); 113  sendImage(bm); 114  } 115  } 116 117 private void sendImage(Bitmap bm) { 118 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 119 bm.compress(Bitmap.CompressFormat.PNG, 60, stream); 120 byte[] bytes = stream.toByteArray(); 121 String img = new String(Base64.encodeToString(bytes, Base64.DEFAULT)); 122 123 AsyncHttpClient client = new AsyncHttpClient(); 124 RequestParams params = new RequestParams(); 125 params.add("img", img); 126 client.post("http://192.168.56.1/ImgUpload.php", params, new AsyncHttpResponseHandler() { 127  @Override 128 public void onSuccess(int i, Header[] headers, byte[] bytes) { 129 Toast.makeText(MainActivity.this, "Upload Success!", Toast.LENGTH_LONG).show(); 130 131  } 132 133  @Override 134 public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { 135 Toast.makeText(MainActivity.this, "Upload Fail!", Toast.LENGTH_LONG).show(); 136  } 137  }); 138  } 139 140  @Override 141 public boolean onCreateOptionsMenu(Menu menu) { 142 // Inflate the menu; this adds items to the action bar if it is present. 143  getMenuInflater().inflate(R.menu.menu_main, menu); 144 return true; 145  } 146 147  @Override 148 public boolean onOptionsItemSelected(MenuItem item) { 149 // Handle action bar item clicks here. The action bar will 150 // automatically handle clicks on the Home/Up button, so long 151 // as you specify a parent activity in AndroidManifest.xml. 152 int id = item.getItemId(); 153 154 //noinspection SimplifiableIfStatement 155 if (id == R.id.action_settings) { 156 return true; 157  } 158 159 return super.onOptionsItemSelected(item); 160  } 161 }

更多相关文章

  1. android核心技术之ANR分析(MTK)
  2. 把android 主板打造成ip摄像头
  3. android打开前置摄像头和后置摄像头
  4. android camera(一):camera模组CMM介绍
  5. Android(安卓)关于获取摄像头帧数据
  6. Android保存图片到系统相册
  7. Android调用系统摄像头拍照并剪裁压缩
  8. Android调用系统摄像头拍照并剪裁压缩
  9. Android(安卓)关于获取摄像头帧数据

随机推荐

  1. 【Android应用开发】分享一个录制 Androi
  2. 解读Android(安卓)LOG机制的实现
  3. 进程和线程模型(android)
  4. 从我的android说起
  5. android 可用内存的阀值--转载
  6. Android屏幕手势检测的实现代码
  7. android进程和线程模型
  8. 两款免费Android日文词典软件
  9. Android(安卓)系统内存扫盲点
  10. Android(安卓)系统分析[1]