pkga/app/src/main/java/com/psmreborn/pkga/DownloadImage.java

54 lines
1.8 KiB
Java

package com.psmreborn.pkga;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class DownloadImage {
static void download(Context context, String url, ImageView imageView, File cacheIcon, String titleId) {
Handler handler = new Handler(context.getMainLooper());
new Thread(() -> {
try {
InputStream in = new URL(url).openStream();
OutputStream out = new FileOutputStream(cacheIcon);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
final Bitmap icon = BitmapFactory.decodeFile(cacheIcon.getAbsolutePath());
handler.post(() -> {
String tag = (String)imageView.getTag();
if(!titleId.equals(tag)) {
return;
}
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setDuration(250);
animation.setFillAfter(true);
imageView.setAnimation(animation);
imageView.setImageBitmap(icon);
});
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}).start();
}
}