我在服務器上有多個圖像。我想要做的是我想從服務器檢索這些圖像,並將其設置在imageView上。從服務器獲取位圖圖像以在ImageView上設置
我已經從服務器獲取blob類型的圖像,將其解碼爲字節數組,然後將其轉換爲位圖圖像。
我很困惑如何從服務器獲取位圖圖像。
我經歷過很多問題上如此喜歡
Retriving image from server to android app
Load Large Image from server on Android
請任何一個可以幫助的鏈接或代碼。
謝謝你寶貴的時間。
我嘗試的東西,這裏是代碼:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
BufferedInputStream bis = new BufferedInputStream(in, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
System.out.println("BAF= "+baf);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
System.out.println("imageData= "+imageData);
bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
in.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
return bitmap;
}
}
這將返回形象,但他的網址,我已經使用由映像名稱。如果我有更多的圖像,並且想要在應用程序加載時檢索所有圖像,該怎麼辦?
檢查本教程中有關如何從服務器異步使用的AsyncTask下載圖片:http://www.android-ios-tutorials.com/182/show-progressbar-while-download-image-using-asynctask-in-android/ – Houcine
凌空,畢加索,素...... – njzk2
謝謝大家的回答。但每個人都提出了不同的技巧,而我對所有這些都是陌生的,這讓我更加困惑。 – Droid