简体   繁体   English

Android从JSON获取Image.png

[英]Android Get Image.png from JSON

I have a JSON that contains strings and image.png objects. 我有一个包含字符串和image.png对象的JSON。

I can get the string the usual way, but how do I get the Images? 我可以按通常方式获取字符串,但是如何获取图片? All tutorials I found use jsons with specific image URLs instead of the png. 我发现的所有教程都使用具有特定图像URL的json而不是png。 Look at the JSON and you will understand it: 查看JSON,您将了解它:

{
"draw": 0,
"recordsTotal": 8,
"recordsFiltered": 8,
"data": [{
    "start_date": "2017-03-08 17:45:00",
    "competition_name": "Primera Divisi\u00f3n",
    "competition_logo": "laliga.png",
    "home_team": "Deportivo La Coru\u00f1a",
    "home_team_logo": "deportivo-la-coruna-2018.png",
    "home_team_slug": "deportivo-la-coruna-640",
    "event_status": "",
    "away_team": "Real Betis",
    "away_team_logo": "real-betis-2025.png",
    "away_team_slug": "real-betis-639",
    "event_id": "3404",
    "game_minute": ""
},

My code has the usual format to get json objects from array. 我的代码具有从数组中获取json对象的常用格式。

try {
            JSONObject jsonObject = new JSONObject(result);

            if (jsonObject.length() > 0) {
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                if (jsonArray.length() > 0){
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonPart = jsonArray.getJSONObject(i);
                        String start_date= jsonPart.getString("start_date");

I want the home_team_logo and away_team_logo images to put on a ImageView. 我希望将home_team_logo和away_team_logo图像放在ImageView上。

As far as you write above, you can get the PNG files name just like this: 就您在上面编写的内容而言,您可以像下面这样获得PNG文件的名称:

String start_date= jsonPart.getString("home_team_logo");

May be the problem is you want show the picture in the view, I think first you want add the URL prefix where from you load the JSON result, and then pass the URL to Glide/ImageLoader like framework to show it on the imageView. 可能是问题在于您想在视图中显示图片,我想首先要在加载JSON结果的位置添加URL前缀,然后将该URL传递给Glide / ImageLoader之类的框架,以在imageView上显示它。

If you have the path in which those images are, you can just download the images in asynchronous manner, like so: 如果您拥有这些图像所在的路径,则可以以异步方式下载图像,如下所示:

import java.lang.ref.WeakReference;

public static class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public ImageDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return downloadBitmap(params[0], params[1]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            } else {
                Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.potch_app_logo_icon);
                imageView.setImageDrawable(placeholder);
            }
        }
    }
}
private static Bitmap downloadBitmap(String url, String localPath) {
    HttpURLConnection urlConnection = null;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();
        int statusCode = urlConnection.getResponseCode();
        if (statusCode != 200) {
            return null;
        }
        InputStream inputStream = urlConnection.getInputStream();
        if (inputStream != null) {
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(localPath));
            return bitmap;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.w("ImageDownloader", "Error downloading image from " + url);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;
}

Then in your code: 然后在您的代码中:

String away_tema_logo_image_name = jsonPart.getString("away_team_logo");
new ImageDownloaderTask(yourImageView).execute(yourUrl, yourLocalPath);

This will save the image to local storage on device, so you need 这会将图像保存到设备的本地存储中,因此您需要

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM