您的位置:首页 > 移动开发 > Android开发

解决通过Intent调用系统拍照程序,返回图片太小的问题[android]

2015-07-08 15:53 891 查看
转载自:http://zjf1428.iteye.com/blog/919162

以下的代码可以调用系统的拍照程序,

Intent it = newIntent("android.media.action.IMAGE_CAPTURE");

startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);

按下拍照键后,会返回到你的activity,所以你的activity要在onActivityResult方法里加一个处理,

protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

try{

Bundle extras = data.getExtras();

Bitmap b = (Bitmap) extras.get("data");

take = b;

ImageView img = (ImageView)findViewById(R.id.image);

img.setImageBitmap(take);

}catch(Exception e){

}

}

但是这样你会发现这个bitmap尺寸太小了。明显是被压缩过了,要像返回未被压缩的照片,那么你要给调用系统拍照程序intent加上参数,指定图片输出的位置。

1

2

3

Intent it = newIntent("android.media.action.IMAGE_CAPTURE");

it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile(F.SD_CARD_TEMP_PHOTO_PATH)));

startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);

这样就是大图片返回了。

protectedvoidonActivityResult(intrequestCode, intresultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

try{

ImageView img = (ImageView)findViewById(R.id.image);

take = U.ResizeBitmap(U.getBitmapForFile(F.SD_CARD_TEMP_PHOTO_PATH), 640);

img.setImageBitmap(take);

imgflag = true;

}catch(Exception e){

}

}

另外注意一下,返回的那个bitmap会很大,你用完以后要把它回收掉,不然你很容易内存报oom错误

publicstaticBitmap ResizeBitmap(Bitmap bitmap, intnewWidth) {

intwidth = bitmap.getWidth();

intheight = bitmap.getHeight();

floattemp = ((float) height) / ((float) width);

intnewHeight = (int) ((newWidth) * temp);

floatscaleWidth = ((float) newWidth) / width;

floatscaleHeight = ((float) newHeight) / height;

Matrix matrix = newMatrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// matrix.postRotate(45);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

bitmap.recycle();

returnresizedBitmap;

}

完整版

java类代码:

Java代码


package com.android.camera;

import java.io.File;

import java.io.InputStreamReader;

import com.android.R;

import com.utils.CommonUtil;

import com.utils.ExceptionDetail;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

/**

* 调用系统相机拍照保存图片并浏览图片

*

*/

public class CameraTest8 extends Activity implements OnClickListener{

private String PHOTO_FOLDER=new File(Environment.getExternalStorageDirectory(), "").getPath()+"/myAndroid/";

private String PHOTO_NAME="";

private ImageView imageView_pic;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.cameratest8);

File file=new File(PHOTO_FOLDER);

if (!file.exists() && !file.isDirectory()) {

file.mkdir();

}

Button button_camera=(Button)findViewById(R.id.button_camera);

button_camera.setOnClickListener(this);

imageView_pic=(ImageView)findViewById(R.id.imageView_pic);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode){

case Activity.DEFAULT_KEYS_DIALER:{

try{

Bitmap take = resizeBitmap(getBitmapForFile(PHOTO_FOLDER+PHOTO_NAME), 640);

imageView_pic.setImageBitmap(take);

}catch(Exception e){

System.out.println(ExceptionDetail.getErrorMessage(e));

}

break;

}

}

}

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

float temp = ((float) height) / ((float) width);

int newHeight = (int) ((newWidth) * temp);

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// matrix.postRotate(45);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

bitmap.recycle();

return resizedBitmap;

}

public static Bitmap getBitmapForFile(String filePath){

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

return bitmap;

}

@Override

public void onClick(View v) {

switch(v.getId()){

case R.id.button_camera:{

PHOTO_NAME="MyPic"+CommonUtil.getCurrentTime("yyyyMMddHHmmssSSS")+".jpg";

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PHOTO_FOLDER+PHOTO_NAME)));

startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);

break;

}

}

}

}

布局文件代码:

Xml代码


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<Button

android:id="@+id/button_camera"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="相机" />

<ImageView

android:id="@+id/imageView_pic"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button_camera"

android:layout_centerHorizontal="true"

android:layout_marginTop="60dp"

android:src="@drawable/app_panel_friendcard_icon" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: