1.XML布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" 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="com.example.administrator.myapplication.MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello" /> <ImageView android:id="@+id/imageID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:maxHeight="50dip" android:maxWidth="50dip" /> <Button android:id="@+id/btn_01" android:layout_width="150dip" android:layout_height="50dip" android:text="相册" /> <Button android:id="@+id/btn_02" android:layout_width="150dip" android:layout_height="50dip" android:text="拍照" /> </LinearLayout>
2.Activity
1 package com.example.administrator.myapplication; 2 3 import android.content.Intent; 4 import android.graphics.Bitmap; 5 import android.net.Uri; 6 import android.os.Environment; 7 import android.provider.MediaStore; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.ImageView; 13 14 import java.io.ByteArrayOutputStream; 15 import java.io.File; 16 17 public class MainActivity extends AppCompatActivity { 18 19 public static final int NONE = 0; 20 public static final int PHOTOHRAPH = 1;// 拍照 21 public static final int PHOTOZOOM = 2; // 缩放 22 public static final int PHOTORESOULT = 3;// 结果 23 24 public static final String IMAGE_UNSPECIFIED = "image/*"; 25 ImageView imageView = null; 26 Button button0 = null; 27 Button button1 = null; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 imageView = (ImageView) findViewById(R.id.imageID); 33 button0 = (Button) findViewById(R.id.btn_01); 34 button1 = (Button) findViewById(R.id.btn_02); 35 36 button0.setOnClickListener(new View.OnClickListener() { 37 @Override 38 public void onClick(View v) { 39 Intent intent = new Intent(Intent.ACTION_PICK, null); 40 intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); 41 startActivityForResult(intent, PHOTOZOOM); 42 } 43 }); 44 45 button1.setOnClickListener(new View.OnClickListener() { 46 47 @Override 48 public void onClick(View v) { 49 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 50 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"))); 51 startActivityForResult(intent, PHOTOHRAPH); 52 } 53 }); 54 } 55 56 @Override 57 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 58 if (resultCode == NONE) 59 return; 60 // 拍照 61 if (requestCode == PHOTOHRAPH) { 62 //设置文件保存路径这里放在跟目录下 63 File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); 64 startPhotoZoom(Uri.fromFile(picture)); 65 } 66 67 if (data == null) 68 return; 69 70 // 读取相册缩放图片 71 if (requestCode == PHOTOZOOM) { 72 startPhotoZoom(data.getData()); 73 } 74 // 处理结果 75 if (requestCode == PHOTORESOULT) { 76 Bundle extras = data.getExtras(); 77 if (extras != null) { 78 Bitmap photo = extras.getParcelable("data"); 79 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 80 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件 81 imageView.setImageBitmap(photo); 82 } 83 84 } 85 86 super.onActivityResult(requestCode, resultCode, data); 87 } 88 89 public void startPhotoZoom(Uri uri) { 90 Intent intent = new Intent("com.android.camera.action.CROP"); 91 intent.setDataAndType(uri, IMAGE_UNSPECIFIED); 92 intent.putExtra("crop", "true"); 93 // aspectX aspectY 是宽高的比例 94 intent.putExtra("aspectX", 1); 95 intent.putExtra("aspectY", 1); 96 // outputX outputY 是裁剪图片宽高 97 intent.putExtra("outputX", 64); 98 intent.putExtra("outputY", 64); 99 intent.putExtra("return-data", true); 100 startActivityForResult(intent, PHOTORESOULT); 101 } 102 }
--------------------------
package com.example.administrator.myapplicationphotochange; import android.content.ContentResolver; import android.content.Intent; import android.content.SyncStatusObserver; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.File; public class MainActivity extends AppCompatActivity { public static final int NONE = 0; public static final int PHOTOHRAPH = 1;// 拍照 public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESOULT = 3;// 结果 public static final String IMAGE_UNSPECIFIED = "image/*"; ImageView imageView = null; Button button0 = null; Button button1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageID); button0 = (Button) findViewById(R.id.btn_01); button1 = (Button) findViewById(R.id.btn_02); button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent, PHOTOZOOM); } }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(intent, PHOTOHRAPH); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; // 拍照 if (requestCode == PHOTOHRAPH) { //设置文件保存路径这里放在跟目录下 File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } if (data == null) return; // 读取相册缩放图片 if (requestCode == PHOTOZOOM) { startPhotoZoom(data.getData()); ContentResolver resolver = getContentResolver(); Uri originalUri = data.getData(); //获得图片的uri // Bitmap bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(originalUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); String path1=Environment.getExternalStorageDirectory().getAbsolutePath(); Log.d("path**1",path1); Log.d("path**",path); } // 处理结果 if (requestCode == PHOTORESOULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件 imageView.setImageBitmap(photo); } } super.onActivityResult(requestCode, resultCode, data); } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); } }