package com.only.android.app; 02 03 import java.io.File; 04 05 import android.app.Activity; 06 import android.app.AlertDialog; 07 import android.content.DialogInterface; 08 import android.content.Intent; 09 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.os.SystemClock; 14 import android.provider.MediaStore; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.ImageView; 18 19 import com.only.android.R; 20 21 public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener { 22 23 private Button selectImageBtn; 24 private ImageView imageView; 25 26 private File sdcardTempFile; 27 private AlertDialog dialog; 28 private int crop = 180 ; 29 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 super .onCreate(savedInstanceState); 33 setContentView(R.layout.imagescale); 34 35 selectImageBtn = (Button) findViewById(R.id.selectImageBtn); 36 imageView = (ImageView) findViewById(R.id.imageView); 37 38 selectImageBtn.setOnClickListener( this ); 39 sdcardTempFile = new File( "/mnt/sdcard/" , "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg" ); 40 41 } 42 43 @Override 44 public void onClick(View v) { 45 if (v == selectImageBtn) { 46 if (dialog == null ) { 47 dialog = new AlertDialog.Builder( this ).setItems( new String[] { "相機" , "相冊" }, new DialogInterface.OnClickListener() { 48 @Override 49 public void onClick(DialogInterface dialog, int which) { 50 if (which == ) { 51 Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE" ); 52 intent.putExtra( "output" , Uri.fromFile(sdcardTempFile)); 53 intent.putExtra( "crop" , "true" ); 54 intent.putExtra( "aspectX" , 1 ); // 裁剪框比例 55 intent.putExtra( "aspectY" , 1 ); 56 intent.putExtra( "outputX" , crop); // 輸出圖檔大小 57 intent.putExtra( "outputY" , crop); 58 startActivityForResult(intent, 101 ); 59 } else { 60 Intent intent = new Intent( "android.intent.action.PICK" ); 61 intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*" ); 62 intent.putExtra( "output" , Uri.fromFile(sdcardTempFile)); 63 intent.putExtra( "crop" , "true" ); 64 intent.putExtra( "aspectX" , 1 ); // 裁剪框比例 65 intent.putExtra( "aspectY" , 1 ); 66 intent.putExtra( "outputX" , crop); // 輸出圖檔大小 67 intent.putExtra( "outputY" , crop); 68 startActivityForResult(intent, 100 ); 69 } 70 } 71 }).create(); 72 } 73 if (!dialog.isShowing()) { 74 dialog.show(); 75 } 76 } 77 } 78 79 @Override 80 protected void onActivityResult( int requestCode, int resultCode, Intent intent) { 81 if (resultCode == RESULT_OK) { 82 Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath()); 83 imageView.setImageBitmap(bmp); 84 } 85 } 86 }