- Recently, I am doing WeChat sharing product webpage links, and the url of the image needs to be converted to byte[], so the Bitmap must be generated first.
- Glide 4.9 version
- Configure network permissions
<uses-permission android:name="android.permission.INTERNET"/>
- Import the Glide library
implementation 'com.github.bumptech.glide:glide:4.9.0'
- In version 4.9, many of the previous methods are obsolete. With the submit() method, a new thread must be opened. Below is the MainActivity code
public class MainActivity extends AppCompatActivity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.iv);
final String url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3738301997,3763137318&fm=26&gp=0.jpg";
new Thread(new Runnable() {
@Override
public void run() {
try {
final Bitmap b = Glide.with(MainActivity.this)
.asBitmap()
.load(url)
.submit()
.get();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "图片像素:" + b.getWidth() + "x" + b.getHeight() + ",开始微信分享图片或者网页链接", Toast.LENGTH_SHORT).show();
}
});
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
final effect: