图片获取保存,使用封装的相册

2017-02-22  本文已影响669人  若兮生生

1.我实现的效果

a:从相册选择图片
b:图片裁剪
c:保存图片到本地
d:再次进入显示图片

2.使用到的依赖

compile 'com.jph.takephoto:takephoto_library:4.0.3'

该依赖的github地址:https://github.com/crazycodeboy/TakePhoto

3.该依赖的示例,写的挺详细,

one.jpg

其中MainActivity是选择fragment还是Activity.我这里选择的是Activity,其中SimpleActivity是拿到图片的,只不过示例是在ResultActivity里面展示的。而对图片的压缩裁剪等都是在CustomHelper里面进行操作的。这里提供了压缩裁剪工具等的选择,而我在项目中是写死的,因为我不需要选择。

4.在以上基础上,我进行了修改。

a. 新建PhotoHelper类,用于获得图片的处理方式。

public class PhotoHelper {
    private View rootView;


    public PhotoHelper(View rootView) {
        this.rootView = rootView;
    }

    public static PhotoHelper of(View rootView,Context context){
        return new PhotoHelper(rootView);
    }

    public void onClick(View view, TakePhoto photo){
        File file=new File(Environment.getExternalStorageDirectory(),
                "/temp"+ System.currentTimeMillis()+".jpg");

        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        Uri imageUri=Uri.fromFile(file);

        configCompress(photo);
        configTakePhotoOption(photo);
        photo.onPickFromGalleryWithCrop(imageUri,getCropOptions());
    }
//裁剪图片属性
    private CropOptions getCropOptions() {
        CropOptions.Builder builder=new CropOptions.Builder();
        builder.setAspectX(800).setAspectY(800);//裁剪时的尺寸比例
        builder.setWithOwnCrop(true);//s使用第三方还是takephoto自带的裁剪工具
        return builder.create();
    }
    
//配置图片属性
    private void configTakePhotoOption(TakePhoto photo) {
        TakePhotoOptions.Builder builder=new TakePhotoOptions.Builder();
        builder.setWithOwnGallery(true);//使用自带相册
        builder.setCorrectImage(false);//纠正旋转角度
        photo.setTakePhotoOptions(builder.create());
    }
    
//    配置压缩
    private void configCompress(TakePhoto takePhoto){
        CompressConfig config=new CompressConfig.Builder()
                .setMaxSize(102400)//大小不超过100k
                .setMaxPixel(800)//最大像素800
                .enableReserveRaw(true)//是否压缩
                .create();
        takePhoto.onEnableCompress(config,true);//这个trued代表显示压缩进度条
    }
}

在onClick方法中,调用配置的各种属性。

b. MainActivity用于展示图片。首先MainActivity要继承TakePhotoActivity,并重写TakePhotoActivity里面的几个方法。

public class MainActivity  extends TakePhotoActivity {
    private static final String TAG = "MainActivity";
    ImageView img;
    Button button;
    String path;
    Bitmap bitmap;
    PhotoHelper photoHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView= LayoutInflater.from(this).inflate(R.layout.activity_main,null);
        setContentView(contentView);

        photoHelper=PhotoHelper.of(contentView,this);

        button= (Button) contentView.findViewById(R.id.btn);
        img= (ImageView) contentView.findViewById(R.id.img);

        if (MyUtils.getBitmap(this)!=null){
            img.setImageBitmap(MyUtils.getBitmap(this));
        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//这步由于封装就直接进入选择图片界面了。
                photoHelper.onClick(view,getTakePhoto());
            }
        });
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        showImg(result.getImages());
    }

    private void showImg(final ArrayList<TImage> images) {

            Log.e(TAG, "showImg: "+images.get(images.size() - 1).getCompressPath());
            path=images.get(images.size() - 1).getCompressPath();

            Picasso.with(this).load(new File(path))
                    .into(img);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        bitmap = Picasso.with(getApplicationContext())
                                .load(new File(path))
                                .resize(MyUtils.dpToPx(getApplicationContext(),200),MyUtils.dpToPx(getApplicationContext(),200))
                                .transform(new CircleTransform())
                                .get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (bitmap!=null){
                        Log.e(TAG, "run: "+"bitmap不为空,进行保存" );
                        MyUtils.savaImage(getApplication(),bitmap);
                    }
                }
            }).start();
    }
}

这里面关键的地方
一个是在按钮的监听事件里面调用之前图片处理方式的配置,
一个是takeSuccess方法里面调用展示图片的方法。
本来使用picasso,可以直接展示图片的,但为了能下次进入可以直接显示图片,我使用picasso将图片地址转换成bitmap,并保存在sd卡中。

c. 这是保存图片以及获取图片的一个工具类

public class MyUtils {
    private static final String TAG = "MyUtils";
    public static String fileDir;
    public  static File  file;
    
//    根据filePath获得bitmap
    public static Bitmap getBitmap(Context c){
        String filePath=c.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
                .getAbsolutePath()+ File.separator+"head.jpg";
        Log.e(TAG, "getBitmap: "+filePath );
        Bitmap bitmap= BitmapFactory.decodeFile(filePath);
        return bitmap;
    }
    
    public static int dpToPx(Context context, int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }
    
//  保存图片  
    public static void savaImage(Context c,Bitmap bitmap) {
        fileDir=c.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();
        Log.e(TAG, "savaImage: "+fileDir);
        file= new File(fileDir,"head.jpg");
        Log.e(TAG, "savaImage: "+file.toString() );
        if (file.exists()){
            file.delete();
        }
        try {
            FileOutputStream out=new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

d.这是一个使用picasso把图片变圆形的方法。

public class CircleTransform implements Transformation {

    @Override
    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

    @Override
    public String key() {
        return "circle";
    }
}

5.总结
安卓手机具有多样化的图片裁剪方式和获取图片的方法,很难做到使用系统自带的方法完美适配各种手机。于是使用封装的相册便是一个很好的选择。

上一篇 下一篇

猜你喜欢

热点阅读