android相机自动压缩,Android拍照得到全尺寸图片并进行压缩

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/take_photo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Take Photo" />

android:id="@+id/get_photo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="get Photo" />

android:id="@+id/picture"

android:layout_width="300dp"

android:layout_height="300dp"

android:layout_gravity="center_horizontal" />

package com.example.choosepictest;

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

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.provider.MediaStore;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

static final int REQUEST_IMAGE_CAPTURE = 1;

private Button takePhoto;

private Button getPhoto;

private ImageView picture;

private Uri imgUri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

takePhoto = (Button) findViewById(R.id.take_photo);

getPhoto = (Button) findViewById(R.id.get_photo);

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

takePhoto.setOnClickListener(this);

getPhoto.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.take_photo:

dispatchTakePictureIntent();

break;

default:

break;

}

}

// 保存全尺寸照片

String mCurrentPhotoPath;

private void dispatchTakePictureIntent() {

File appDir = new File(Environment.getExternalStorageDirectory(),

"/etoury/picCache");

if (!appDir.exists()) {

appDir.mkdir();

}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")

.format(new Date());

String fileName = timeStamp + ".jpg";

File outputImage = new File(appDir, fileName);

try {

if (outputImage.exists()) {

outputImage.delete();

}

outputImage.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

mCurrentPhotoPath = outputImage.getAbsolutePath();

imgUri = Uri.fromFile(outputImage);

// 意图 相机

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

// 如果有相机

if (intent.resolveActivity(getPackageManager()) != null) {

startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

}

}

//解码缩放图片(Decode a Scaled Image)

private void setPic() {

// Get the dimensions of the View

int targetW = picture.getWidth();

int targetH = picture.getHeight();

// Get the dimensions of the bitmap

BitmapFactory.Options bmOptions = new BitmapFactory.Options();

// 该 值设为true那么将不返回实际的bitmap,也不给其分配内存空间这样就避免内存溢出了。但是允许我们查询图片的信息这其中就包括图片大小信息

bmOptions.inJustDecodeBounds = true;

BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

int photoW = bmOptions.outWidth;

int photoH = bmOptions.outHeight;

// Determine how much to scale down the image

// Math.min求最小值

int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

// Decode the image file into a Bitmap sized to fill the View

bmOptions.inJustDecodeBounds = false;

// 设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误

bmOptions.inSampleSize = scaleFactor;

// 如果inPurgeable设为True的话表示使用BitmapFactory创建的Bitmap,用于存储Pixel的内存空间在系统内存不足时可以被回收

bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

picture.setImageBitmap(bitmap);

}

@Override

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

if (resultCode == RESULT_OK) {

switch (requestCode) {

case REQUEST_IMAGE_CAPTURE:

setPic();

break;

default:

break;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值