![](/img/trans.png)
[英]How to set background image to activity layout using Picasso Library in android
[英]How to compress image using Picasso Library for Android?
我的应用程序使用Camera Intent捕获图像。 图像以“abc.jpg”的形式保存到文件中。 现在使用Picasso Library我尝试通过调整大小来压缩图像。 我没有任何输出。 我的代码永远不会到达Target的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) {
}
});
}
}
}
您不应将Target用作匿名类。 为它定义一个对象并将此引用传递给into()
函数。 例如:
Target myTarget = new Target(){...}
接着,
Picasso.with(context) .load(_path).resize(size, size).centerCrop().into(myTarget);
这样做的原因是anonym类提供了一个非常弱的引用,这意味着Target会收集垃圾。
告诉我它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.