android 选择图片后对图片压缩

android 选择图片压缩
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView mIvPick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mIvPick = (ImageView) findViewById(R.id.iv);
}

public void btPick(View view) {

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, 4);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 4) {
        if (resultCode == RESULT_OK) {
            // 获取图片路径

            Uri uri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                        this.getContentResolver(), uri);
                // BitmapFactory.Options options = new Options();
                // options.inJustDecodeBounds = false;
                //
                // options.inSampleSize = Math.max(bitmap.getWidth(),
                // bitmap.getHeight());
                //

                System.out.println("压缩前" + bitmap.getByteCount());
                // 压缩后

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = this.getContentResolver().query(uri,
                    filePathColumn, null, null,null);               cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            int wh[] = { 50, 90 };
                System.out.println("压缩后"+createNewBitmapAndCompressByFile(picturePath,wh).getByteCount());                                  mIvPick.setImageBitmap(createNewBitmapAndCompressByFile(
                    picturePath, wh));


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

/**
*图片路径压缩
*
*/

public Bitmap createNewBitmapAndCompressByFile(String filePath, int wh[]) {
    int offset = 100;
    File file = new File(filePath);
    long fileSize = file.length();
    if (200 * 1024 < fileSize && fileSize <= 1024 * 1024)
        offset = 90;
    else if (1024 * 1024 < fileSize)
        offset = 85;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; // 为true里只读图片的信息,如果长宽,返回的bitmap为null
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = false;

    BitmapFactory.decodeFile(filePath, options);

    int bmpheight = options.outHeight;
    int bmpWidth = options.outWidth;
    int inSampleSize = bmpheight / wh[1] > bmpWidth / wh[0] ? bmpheight
            / wh[1] : bmpWidth / wh[0];
    // if(bmpheight / wh[1] < bmpWidth / wh[0]) inSampleSize = inSampleSize
    // * 2 / 3;//TODO 如果图片太宽而高度太小,则压缩比例太大。所以乘以2/3
    if (inSampleSize > 1)
        options.inSampleSize = inSampleSize;// 设置缩放比例
    options.inJustDecodeBounds = false;

    InputStream is = null;
    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        return null;
    }
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(is, null, options);
    } catch (OutOfMemoryError e) {

        System.gc();
        bitmap = null;
    }
    if (offset == 100)
        return bitmap;// 缩小质量
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, offset, baos);
    byte[] buffer = baos.toByteArray();
    options = null;
    if (buffer.length >= fileSize)
        return bitmap;
    return BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值