仿微信头像图片裁剪并压缩到100K以下到本地

逻辑:1、打开本地相机-并传入path路径
          2、在onActivityResult中,判断File(path)是否为空,不为空则跳转到下个界面
          PS:这里为什么要用path而不用onActivity返回的bitmap对象呢。 因为返回的bitmap对象是没有path路径的图片清晰
          3、利用ClipImageLayout来对图片进行裁剪--并获取到裁剪完毕之后的图片    
     4、对图片进行质量压缩,并保存到本地

     PS:第四部的时候,一定要压缩之后们通过流的方式保存到本地,不要在调用BitmapFactory.decodeStream方法,否则又会回到200K


import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
      private File file; // 要保存的文件sss
      private Button btnCamera;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout. activity_main);
             btnCamera = (Button) findViewById(R.id. btn_camera);

             btnCamera.setOnClickListener( new OnClickListener() {

                   @Override
                   public void onClick(View v) {
                         file = new File(FileUtils.getImgStorageDirectory() + "/aa.jpg");
                        Intent intent = new Intent();
                        intent.setAction( "android.media.action.IMAGE_CAPTURE");
                        Uri uri = Uri. fromFile(file);
                        intent.putExtra(MediaStore. EXTRA_OUTPUT, uri);
                        startActivityForResult(intent, 100);

                  }
            });

      }

      @Override
      protected void onActivityResult( int requestCode, int resultCode, Intent data) {
             super.onActivityResult(requestCode, resultCode, data);
             if (requestCode == 100 && file.exists() && file.length() > 0) {
                   // 这里吧文件的路径传入到下一个界面,就是裁剪的界面
                  Intent intent = new Intent(MainActivity. this, CropActivity.class);
                  intent.putExtra( "path", file.getPath());
                  startActivity(intent);
            }
      }

}


第二个界面:裁剪的界面:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.ima.view.ClipImageLayout;

public class CropActivity extends Activity implements OnClickListener {
      private Button btn;
      private ClipImageLayout mClipImageLayout;
      private String path;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout. activity_crop);
             path = getIntent().getStringExtra( "path");
             btn = (Button) findViewById(R.id. crop_button);
             btn.setOnClickListener(CropActivity. this);
             mClipImageLayout = (ClipImageLayout) findViewById(R.id.clipImageLayout );

             // 有的系统返回的图片是旋转了,有的没有旋转,所以处理
             int degreee = ImageUtil. readPictureDegree(path);
            Bitmap bitmap = ImageUtil. getSmallBitmap(path);
             if (bitmap != null) {
                   if (degreee == 0) {
                         mClipImageLayout.setImageBitmap(bitmap);
                  } else {
                         mClipImageLayout.setImageBitmap(ImageUtil. rotaingBitmap(degreee, bitmap));
                  }
            }

      }

      @Override
      public void onClick(View v) {
             //得到裁剪的图片
            Bitmap bitmap = mClipImageLayout.clip();
            String imageName = String.valueOf(System.currentTimeMillis());
             //这里把裁剪过后的图片进行质量压缩,并且保存到本地
            ImageUtil. compressAndSave(bitmap, imageName);
            Intent intent = new Intent(CropActivity. this, ShowActivity.class);
            intent.putExtra( "imageName", imageName);
            startActivity(intent);
      }

}
这个类的布局文件:


    

DEMO下载地址:http://download.csdn.net/detail/q908555281/9311177

你可能感兴趣的:(Android,进阶)