这段代码适合“初学者”^^
private Bitmap FixBitmap;
Bitmap thePicBitmap;
String ImagePath = "image_path";
String ImagePath_1;
private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";
private static final int REQ_CODE_PICK_IMAGE = 0;
@Override
public void onClick(View view) {
try {
//intent = new Intent();
// intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//intent.setAction(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("scale", true);
photoPickerIntent.putExtra("aspectX", 1);
photoPickerIntent.putExtra("aspectY", 1);
// indicate output X and Y
photoPickerIntent.putExtra("outputX", 150);
photoPickerIntent.putExtra("outputY", 100);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);
} catch (Exception e) {
// Toast.makeText(getApplicationContext(), R.string.imageException, Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
Toast toast = Toast.makeText(Upload_image_text.this, "This device doesn't support the crop action!",
Toast.LENGTH_SHORT);
toast.show();
}
}
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
if (isSDCARDMounted()) {
File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
}
return f;
} else {
return null;
}
}
private boolean isSDCARDMounted(){
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
});
========================
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case REQ_CODE_PICK_IMAGE:
if (resultCode == RESULT_OK) {
if (imageReturnedIntent!=null){
// File tempFile = getTempFile();
ImagePath_1 = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
System.out.println("path "+ImagePath_1);
FixBitmap = BitmapFactory.decodeFile(ImagePath_1);
ShowSelectedImage = (ImageView)findViewById(R.id.imageView);
ShowSelectedImage.setImageBitmap(FixBitmap);
}
}
}
}
================================
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
//options.outWidth = 50;
//options.outHeight = 50;
FixBitmap = BitmapFactory.decodeFile(ImagePath_1, options);
//FixBitmap = BitmapFactory.decodeResource(getResources(), R.id.imageView);
byteArrayOutputStream = new ByteArrayOutputStream();
FixBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); //compress to 50% of original image quality
byteArray = byteArrayOutputStream.toByteArray();
ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);