Wie zu komprimieren Bild mit Picasso-Bibliothek für Android?
Meine Anwendung erfasst Bild mit Kamera-Vorsatz. Das Bild wird in einer Datei gespeichert, die als "abc.jpg". Jetzt mit Picasso-Bibliothek, die ich versuchen, komprimieren Sie das Bild durch ändern der Größe. I dont get keine Ausgabe. Mein code erreicht nie das Ziel onBitmapLoaded weder onBitmapFailed. Hier ist mein code.
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) {
}
});
}
}
}
suchen können Sie unter diesem link:futurestud.io/blog/picasso-Bild-Anpassung der Skalierung-und-fit
InformationsquelleAutor Bhaskar Gurram | 2016-05-26
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sollten Sie nicht verwenden Ziel als ein anonymer Klasse. Definieren Sie ein Objekt für Sie und übergeben Sie diese Referenz, um die
into()
Funktion. Zum Beispiel:dann,
Der Grund dafür ist, dass ein anonymer Klasse gibt eine wirklich schwache Referenz, was bedeutet, dass das Ziel wird Müll gesammelt.
Mir sagen, ob es geklappt hat.
InformationsquelleAutor Ahmet Kazaman