一、通过相机选图片:


布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" android:gravity="center_horizontal"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="使用系统照相机拍照" android:onClick="click"/><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

代码:

package uk.ac.essex.camerademo1;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.Display;import android.view.View;import android.widget.ImageView;public class Camerademo1Activity extends Activity {    private static final int CAPTURE_PIC = 0;    private ImageView imageView;    private int width;    private int height;    private String imageFilePath;    private Uri imageFileUri;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        imageView = (ImageView) findViewById(R.id.imageView);        init();    }    private void init() {        Display display = getWindowManager().getDefaultDisplay();        width = display.getWidth();        height = display.getHeight();        imageFilePath = Environment.getExternalStorageState().equals(                Environment.MEDIA_MOUNTED) ? Environment                .getExternalStorageDirectory() + "/1.jpg" : null;        imageFileUri = Uri.fromFile(new File(imageFilePath));    }    public void click(View view) {        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 相机捕捉图片的意图        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);// 指定系统相机拍照保存在imageFileUri所指的位置        startActivityForResult(intent, CAPTURE_PIC);// 启动系统相机,等待返回    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_OK && requestCode == CAPTURE_PIC) {            Options options = new Options();            options.inJustDecodeBounds = true;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片            Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸            int widthRatio = (int) Math.ceil(options.outWidth / width);// 获取宽度的压缩比率            int heightRatio = (int) Math.ceil(options.outHeight / height);// 获取高度的压缩比率            if (widthRatio > 1 || heightRatio > 1) {// 只要其中一个的比率大于1,说明需要压缩                if (widthRatio >= heightRatio) {// 取options.inSampleSize为宽高比率中的最大值                    options.inSampleSize = widthRatio;                } else {                    options.inSampleSize = heightRatio;                }            }            options.inJustDecodeBounds = false;// 设置为真正的解码图片            bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码图片            imageView.setImageBitmap(bitmap);        }        super.onActivityResult(requestCode, resultCode, data);    }}

二、通过图库选图片:

public void Camera() {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 相机捕捉图片的意图intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);// 指定系统相机拍照保存在imageFileUri所指的位置startActivityForResult(intent, 1);// 启动系统相机,等待返回}public void OpenImage() {Intent intent = new Intent();intent.setType("image/*");intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(intent, 2);// 打开本地图库}protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == 1 && resultCode == Activity.RESULT_OK) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用Log.i("TestFile", "存储卡读取失败.");return;}// Bundle bundle = data.getExtras();// Bitmap bitmap = (Bitmap) bundle.get("data");//Options options = new Options();options.inJustDecodeBounds = true;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸int widthRatio = (int) Math.ceil(options.outWidth / width);// 获取宽度的压缩比率int heightRatio = (int) Math.ceil(options.outHeight / height);// 获取高度的压缩比率if (widthRatio > 1 || heightRatio > 1) {// 只要其中一个的比率大于1,说明需要压缩if (widthRatio >= heightRatio) {// 取options.inSampleSize为宽高比率中的最大值options.inSampleSize = widthRatio;} else {options.inSampleSize = heightRatio;}}options.inJustDecodeBounds = false;// 设置为真正的解码图片bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码图片}if (requestCode == 2 && resultCode == RESULT_OK && null != data) {Uri uri = data.getData();if (!TextUtils.isEmpty(uri.getAuthority())) {Cursor cursor = getContentResolver().query(uri,new String[] { MediaStore.Images.Media.DATA }, null,null, null);if (null == cursor) {Tools.ToastShort("打开失败,请重试!");return;}cursor.moveToFirst();String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));// 选择的本地图片的路径BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片BitmapFactory.decodeFile(path, opts);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸opts.inSampleSize = 10;opts.inJustDecodeBounds = false;// 设置为真正的解码图片try {Bitmap bmp = BitmapFactory.decodeFile(path, opts);} catch (OutOfMemoryError err) {}

http://www.eoeandroid.com/thread-3222-1-1.html

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=6552&page=1

更多相关文章

  1. Android 在程序界面上显示图片
  2. ImageVIew 设置图片大小
  3. Android: 背景图片平铺要这么干
  4. Android——Gallery 图片拖动效果
  5. Android ImageView图片自适应
  6. android异步图片加载三之handler+线程池+消息队列模式
  7. Android 图片侧滑展示RecyclerView简单实用
  8. Android 制作.9.png图片之利用Android 9-patch shadow generator
  9. Android 图片加载Bitmap OOM错误解决办法

随机推荐

  1. android 状态栏占位_Android(安卓)Studio
  2. Android高德地图使用
  3. Android中Handler Runnable与Thread的区
  4. android批量插入数据效率对比
  5. 如何申请Android(安卓)MapView的apiKey
  6. 【Android程序开发】EditText详解
  7. Android(安卓)兼容性测试(CTS)
  8. android之Buffer类及子类学习
  9. Android(安卓)layout_alignBottom 注意事
  10. Android(安卓)获取联系人