<span style= "color: rgb(0, 0, 255); font-size: 15px;" ><strong> package
com.ting.textcover;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.os.SystemClock;
import
android.provider.MediaStore;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
/**
* 调取系统相机、并将图片展示在ImageView上
* @author siva
*
*/
public
class MainActivity extends
Activity {
private
Button btn ;
private
ImageView image;
private
String pathName;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
image = (ImageView) findViewById(R.id.iamgeView);
pathName = Environment.getExternalStorageDirectory()+ "/image.jpg" ;
btn.setOnClickListener(btnlistener);
}
private
OnClickListener btnlistener = new
OnClickListener() {
@Override
public
void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//用户设置一个临时保存文件的地址,每次都会被替换掉
Uri newFileUri = Uri.fromFile( new
File(pathName));
intent.putExtra(MediaStore.EXTRA_OUTPUT, newFileUri);
startActivityForResult(intent, 1 );
}
};
@Override
protected
void onActivityResult( int
requestCode, int
resultCode, Intent data) {
// TODO Auto-generated method stub
if
(resultCode==RESULT_OK) {
switch
(requestCode) {
case
1 :
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inJustDecodeBounds = true ; //这时候decode的bitmap为null,只是宽高放到其中
Bitmap bitmap = BitmapFactory.decodeFile(pathName,options);
options.inJustDecodeBounds = false ;
//因为是等比例缩放、因此只设置一个即可
int
be = options.outHeight/ 300 ;
if (be<= 0 ){
be= 1 ;
}
options.inSampleSize = be; //缩放比例
//重新读入图片,此时的bitmap不是null值;若此处出现null ,查看pathname是否有问题
bitmap = BitmapFactory.decodeFile(pathName,options);
int
withd = bitmap.getWidth();
int
height = bitmap.getHeight();
System.out.println(withd+ "====" +height);
image.setImageBitmap(bitmap);
// 将图片保存在SD卡上
break ;
default :
break ;
}
}
}
}
</strong></span>
|