安卓imageView加载MYSQL图片,Android Studio将图像URL加载到列表视图中

I want to fill a listview with text and images.

I receive this information by my mysql database, in JSON format.

I have a field called "FOTO" and I store into this the path to the photo like: "http://....../1.png".

I get and android.os.NetworkOnMainThreadException using this code, but I don't know how to do different.

I parse the JSON and pass the values to the listadapter. I need to pass also the icon so the bitmap value, but I need to download it from the server.

public class DisplayListView extends AppCompatActivity {

final static String TAG = "sb.dl";

String json_string;

JSONObject jsonObject;

JSONArray jsonArray;

TeamAdapter teamAdapter;

ListView listView;

Bitmap icon = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.display_listview_layout);

teamAdapter = new TeamAdapter(this, R.layout.row_layout);

listView = (ListView) findViewById(R.id.listview);

listView.setAdapter(teamAdapter);

json_string = getIntent().getExtras().getString("json_data");

Log.d(TAG, "json_string " + json_string);

try {

jsonObject = new JSONObject(json_string);

jsonArray = jsonObject.getJSONArray("risposta");

int count = 0;

String nome, num, data;

while (count < jsonArray.length()) {

JSONObject JO = jsonArray.getJSONObject(count);

nome = JO.getString("NOME");

num = JO.getString("NUMERO");

data = JO.getString("DATA_NASCITA");

String url = JO.getString("FOTO");

icon = LoadImageFromWebOperations(url);

Team team = new Team(nome, num, data, icon);

teamAdapter.add(team);

count++;

}

} catch (JSONException e) {

e.printStackTrace();

Log.d("Simone", e.toString());

Log.d("Simone", e.getMessage());

}

}

public static Bitmap LoadImageFromWebOperations() {

try {

URL url = new URL("url");

Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

return bmp;

} catch (Exception e) {

Log.d(TAG, "bitmap error: " + e.toString());

Log.d(TAG, "bitmap error: " + e.getMessage());

return null;

}

}

}

解决方案

I had the same problem.

I solved with an AsyncTask that download the bitmaps.

I used this code:

class ImageDownloaderTask extends AsyncTask {

private final WeakReference imageViewReference;

public ImageDownloaderTask(ImageView imageView) {

imageViewReference = new WeakReference(imageView);

}

@Override

protected Bitmap doInBackground(String... params) {

return downloadBitmap(params[0]);

}

@Override

protected void onPostExecute(Bitmap bitmap) {

if (isCancelled()) {

bitmap = null;

}

if (imageViewReference != null) {

ImageView imageView = imageViewReference.get();

if (imageView != null) {

if (bitmap != null) {

imageView.setImageBitmap(bitmap);

} else {

Drawable placeholder = null;

imageView.setImageDrawable(placeholder);

}

}

}

}

private Bitmap downloadBitmap(String url) {

HttpURLConnection urlConnection = null;

try {

URL uri = new URL(url);

urlConnection = (HttpURLConnection) uri.openConnection();

final int responseCode = urlConnection.getResponseCode();

if (responseCode != HttpURLConnection.HTTP_OK) {

return null;

}

InputStream inputStream = urlConnection.getInputStream();

if (inputStream != null) {

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

return bitmap;

}

} catch (Exception e) {

urlConnection.disconnect();

Log.w("ImageDownloader", "Errore durante il download da " + url);

} finally {

if (urlConnection != null) {

urlConnection.disconnect();

}

}

return null;

}

}

you call it by:

new ImageDownloaderTask(holder.imageView).execute(urlPhoto);

Hope this help :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值