在互联网的帮助下,我可以下载jpg并将其设置为带位图的ImageView。但是,我想将其保留为点动或png。
我该怎么做?
public class GetFlickr extends AsyncTask<String, String, Bitmap> {
private ProgressBar pBar;
private String file = "flickr.xml";
private WeakReference<ImageView> imageViewWeakReference;
GetFlickr(ImageView Iv){
imageViewWeakReference = new WeakReference<>(Iv);
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pBar = findViewById(R.id.progressBar);
pBar.setProgress(0);
pBar.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap=null;
try{
URL urlfinal = new URL("https://farm66.staticflickr.com/65535/49344706212_80c4e61053.jpg");
HttpURLConnection connection1 = (HttpURLConnection) urlfinal.openConnection();
connection1.connect();
//FileOutputStream fOut2= openFileOutput("flickr.jpg", MODE_PRIVATE);
InputStream fOut2 = urlfinal.openStream();
bitmap = BitmapFactory.decodeStream(fOut2);
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return bitmap;
}
protected void onProgressUpdate(String... progress) {
pBar.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(Bitmap bitmap) {
ImageView Iv = imageViewWeakReference.get();
if (Iv != null){
Iv.setImageBitmap(bitmap);
}
pBar.setVisibility(View.GONE);
}
}
}
一个好的帮助将是库Picasso
您可以轻松处理在线图像,自动缓存等。>
只需插入您的gradle:
implementation 'com.squareup.picasso:picasso:(insert latest version)'
我建议您使用Glide
库。它支持LruCache和diskCache。
dependencies {
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}
然后按以下方式加载: