一、通过相机选图片:

布局文件:

<?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"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用系统照相机拍照" android:onClick="click"/> <ImageView
android: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. 把token带到 http头部 或者验证一下referer
  2. [WCF编程]7.实例上下文模式
  3. Ubuntu 部署 Node.js 应用
  4. TCP IP详解(转)
  5. java.util.ConcurrentModificationException 解决办法
  6. C++读入一个参数
  7. Failed to load unit &#39;HGCM&#39; (VERR_INVALID_PARAMETER)
  8. 巧用Session Manager还原Firefox丢失会话
  9. Love Hotels and Unicode[转]
  10. FMX相当于在界面上自己又做了一个小操作系统
  11. hdu Hat&#39;s Tea
  12. copy和mutableCopy都是浅拷贝!!!------你被骗了很多年
  13. jdbc的入门学习
  14. web api HttpResponseMessage的简单使用
  15. java new关键字
  16. 本机的虚拟机执行ifconfig,显示不出ip的解决方法
  17. 继承ViewGroup学习onMeasure()和onLayout()方法
  18. 【Java】高并发同步Volatile的使用
  19. jQuery的选择器的总结
  20. 安装OpenResty

热门文章

  1. 解决spring-boot-starter-logging与log4j冲突
  2. js学习笔记14----DOM概念及子节点类型
  3. EMS快递单号生成算法
  4. Jquery仿IGoogle实现可拖动窗口(源码)
  5. C++(1)C++类四个默认函数---构造函数、析构函数、拷贝函数、赋值函数
  6. 为什么要把session存入数据库
  7. 关于B/S和C/S模式
  8. RTC终于tm的通了
  9. andriod sdk 安卓模拟器修改imei码,位置信息
  10. [Scikit-learn] Dynamic Bayesian Network - Kalman Filter