-->

如何使用图书馆毕加索为Android压缩图像?(How to compress image usin

2019-09-30 10:03发布

我的应用程序捕获的图像使用相机的意图。 图像保存到文件“abc.jpg的”。 现在用毕加索图书馆我尝试通过调整它来压缩图像。 我没有得到任何输出。 我的代码永远不会达到目标的onBitmapLoaded既不onBitmapFailed。 这里是我的代码。

public static final String DATA_PATH = Environment
.getExternalStorageDirectory() + "/SnapReminder/";
File file1 = new File(DATA_PATH);
file1.mkdirs();
String _path = DATA_PATH + "abc.jpg";
File file = new File(_path);
Uri imageUri = Uri.fromFile(file);
final Intent intent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, 0);



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
  if (resultCode == RESULT_OK) {
  Picasso.with(context)
  .load(_path)
  .resize(size, size)
  .centerCrop()
  .into(new Target() {
       @Override
       public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom
       from) {              
                        File file = new File(_path);
                        try {
                            file.createNewFile();
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                            ostream.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {
                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {
                    }
                });

}
}
}

Answer 1:

你不应该使用目标为化名类。 定义对象,并通过该参考into()函数。 例如:

Target myTarget = new Target(){...}

接着,

Picasso.with(context) .load(_path).resize(size, size).centerCrop().into(myTarget);  

这样做的原因是,一个化名类提供一个非常弱引用,这意味着该目标得到垃圾收集。

告诉我,如果它的工作。



文章来源: How to compress image using Picasso Library for Android?