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 }
时间: 2024-11-06 17:31:11