There are the following fragments for the code of caching pictures to mobile phones
Let's first look at the input stream code
protected Bitmap doInBackground(String... integers) {
//Load the network picture, get a Bitmap object and return to Bitmap
Bitmap bm = null;
try {
//Create URl
URL url = new URL(params[0]);
//openConnection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Create input stream
InputStream inputStream = connection.getInputStream();
//Convert the input stream to a bitmap type object
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
As for the return value of 1, we have the following code to return, and then we will send the object of type bitmap to imageView and display it on the mobile screen
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
switch (result) {
case 1:
imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
As for the previous click event, let's take a look at the following code
public class ShowWebPicActivity extends AppCompatActivity {
private Button showbtn;
private ImageView webimgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_web_pic);
blid();
showbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DownloadImgTask task = new DownloadImgTask(ShowWebPicActivity.this, showbtn, webimgView);
task.execute("http://img31.mtime.cn/mg/2012/10/30/201631.37192876.jpg");
}
});
}
private void blid() {
showbtn = findViewById(R.id.show_btn);
webimgView = findViewById(R.id.pic_img);
}
Click the btn event and run the following two lines of code, and the mobile screen will be loaded on the mobile screen with your linked pictures