http://www.linuxidc.com/Linux/2012-11/73940.htm
这个地址吧问题讲解的很详细~
解决方案也正确~我就是按照那个改的~
核心代码在下方~方便遇到一样问题的同学解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
public class MainActivity extends Activity {
// data
private static final String TAG = "MainActivity";
private static final int TAKE_BIG_PICTURE = 1;
private static final int TAKE_SMALL_PICTURE = 2;
private static final int CROP_BIG_PICTURE = 3;
private static final int CROP_SMALL_PICTURE = 4;
private static final int CHOOSE_BIG_PICTURE = 5;
private static final int CHOOSE_SMALL_PICTURE = 6;
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";
private Uri imageUri;// to store the big bitmap
// views
private ImageView imageView;
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", false);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
/** Handle click events */
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.buttonTakeBigPicture:
if (imageUri == null)
Log.e(TAG, "image uri can't be null");
// capture a big bitmap and store it in Uri
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
// capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_BIG_PICTURE);
break;
case R.id.buttonTakeSmallPicture:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
// capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_SMALL_PICTURE);
break;
case R.id.buttonChooseBigPicture:
intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 300);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
startActivityForResult(intent, CHOOSE_BIG_PICTURE);
break;
case R.id.buttonChooseSmallPicture:
intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 100);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
break;
default:
break;
}
}
/** Activity life cycle */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// views
imageView = (ImageView) findViewById(R.id.imageView);
// instantiate
imageUri = Uri.parse(IMAGE_FILE_LOCATION);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {// result is not correct
Log.e(TAG, "requestCode = " + requestCode);
Log.e(TAG, "resultCode = " + resultCode);
Log.e(TAG, "data = " + data);
return;
} else {
switch (requestCode) {
case TAKE_BIG_PICTURE:
Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);// it seems to be
// null
// TODO sent to crop
cropImageUri(imageUri, 780, 600, CROP_BIG_PICTURE);
// or decode as bitmap and display it
// if(imageUri != null){
// Bitmap bitmap = decodeUriAsBitmap(imageUri);
// imageView.setImageBitmap(bitmap);
// }
break;
case CROP_BIG_PICTURE:// from crop_big_picture
Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);// it seems to be
// null
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
imageView.setImageBitmap(bitmap);
}
break;
case TAKE_SMALL_PICTURE:
Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data);
// TODO sent to crop
cropImageUri(imageUri, 260, 200, CROP_SMALL_PICTURE);
// or decode as bitmap and display it
// if(imageUri != null){
// Bitmap bitmap = decodeUriAsBitmap(imageUri);
// imageView.setImageBitmap(bitmap);
// }
break;
case CROP_SMALL_PICTURE:
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
imageView.setImageBitmap(bitmap);
} else {
Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
}
break;
case CHOOSE_BIG_PICTURE:
Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);// it seems to
// be null
if (imageUri != null) {
Bitmap bitmap = decodeUriAsBitmap(imageUri);
imageView.setImageBitmap(bitmap);
}
break;
case CHOOSE_SMALL_PICTURE:
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);
} else {
Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);
}
break;
default:
break;
}
}
}
}
|
使用Uri来传递图片~就可以避免文件过大的问题~