package com.only.android.app; |
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; |
19 | import com.only.android.R; |
21 | public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener
{ |
22 | /**
Called when the activity is first created. */ |
23 | private Button
selectImageBtn; |
24 | private ImageView
imageView; |
26 | private File
sdcardTempFile; |
27 | private AlertDialog
dialog; |
31 | public void onCreate(Bundle
savedInstanceState) { |
32 | super .onCreate(savedInstanceState); |
33 | setContentView(R.layout.imagescale); |
35 | selectImageBtn
= (Button) findViewById(R.id.selectImageBtn); |
36 | imageView
= (ImageView) findViewById(R.id.imageView); |
38 | selectImageBtn.setOnClickListener( this ); |
39 | sdcardTempFile
= new File( "/mnt/sdcard/" , "tmp_pic_" +
SystemClock.currentThreadTimeMillis() + ".jpg" ); |
44 | public void onClick(View
v) { |
45 | if (v
== selectImageBtn) { |
47 | dialog
= new AlertDialog.Builder( this ).setItems( new String[]
{ "相机" , "相册" }, new DialogInterface.OnClickListener()
{ |
49 | public void onClick(DialogInterface dialog, int 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 ); |
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 ); |
73 | if (!dialog.isShowing())
{ |
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); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理