如何将转换后的图像保存到SD卡

时间:2016-08-26 06:20:28

标签: android imageview

在我的应用程序中,我使用了“picasso-transformations”库来编辑照片。还实现了一个将图像保存到SD卡的功能。我写的用于保存图像的代码保存了原始图像而不是转换。

要编辑的代码

 @Override
            public void onClick(View view, int position) {
                switch (position) {
                    case 0:
                        photo.setImageDrawable(drawable);
                        break;
                    case 1:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new GrayscaleTransformation())
                                .into(photo);
                        break;
                    case 2:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new SepiaFilterTransformation(act))
                                .into(photo);
                        break;
                    case 3:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new ContrastFilterTransformation(act, 2.0f))
                                .into(photo);
                        break;
                    case 4:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new InvertFilterTransformation(act))
                                .into(photo);
                        break;
                    case 5:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new PixelationFilterTransformation(act, 20))
                                .into(photo);
                        break;
                    case 6:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new SketchFilterTransformation(act))
                                .into(photo);
                        break;
                    case 7:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new SwirlFilterTransformation(act, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
                                .into(photo);
                        break;
                    case 8:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new BrightnessFilterTransformation(act, 0.5f))
                                .into(photo);
                        break;
                    case 9:
                        Picasso.with(act)
                                .load(selectedPhotoUri)
                                .transform(new VignetteFilterTransformation(act, new PointF(0.5f, 0.5f),
                                        new float[]{0.0f, 0.0f, 0.0f}, 0f, 0.75f))
                                .into(photo);
                        break;
                    default:
                        photo.setImageDrawable(drawable);
                        break;
                }
            }

如何将图像保存到SD卡

 public void saveImageFile() {
    photo.setDrawingCacheEnabled(true);
    Bitmap bitmap = photo.getDrawingCache();

    String root = Environment.getExternalStorageDirectory().toString();
    File newDir = new File(root + "/Filters");
    newDir.mkdirs();
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);
    String fotoname = "Photo-" + n + ".jpg";
    File file = new File(newDir, fotoname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
        Toast.makeText(act, "Saved to your folder", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {

    }
}

1 个答案:

答案 0 :(得分:1)

向Picasso过程添加回调:

 .into(photo,new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        //Save image method
                    }

                    @Override
                    public void onError() {
                        //Error loading the image
                    }
                });

我希望我已经回答了你的问题:)