我是 Android Studio 的新手。我正在嘗試設計一個活動,在給定 URL 的情況下,我可以單擊某個按鈕,該按鈕允許我提取它在網頁上找到的前 20 張影像。
有沒有辦法在沒有庫的情況下在 Java 中做到這一點?
uj5u.com熱心網友回復:
如果您使用的是網頁 url,那么在 android.here 中使用webview類是網站檔案 https://developer.android.com/reference/android/webkit/WebView
否則你創建自己的布局然后使用2個著名的依賴項:
1.Glide
2.Picasso
uj5u.com熱心網友回復:
你需要異步任務
- 創建位圖變數
- 使用傳遞的 url 打開一個 InputStream
- 在 InputStream 上使用 BitmapFactory.decodeStream()
- 將其分配給創建的 Bitmap 變數
- 然后在 onPostExecute 方法中,您只需在 ImageView 上使用 setImageBitmap
-->
private class DownloadImageFromUrl extends AsyncTask<String, Void,Bitmap> {
ImageView bmImage;
public DownloadImageFromUrl(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406320.html
標籤:
