1

我的应用程序从 JSON 获取图像名称。然后我将该名称连接到另一个 url 以下载图像。我可以下载所有图像,但我下载的所有图像都是相同的。例如,我的图像名称 (1,2,3,4,5,6,7) 但是当我下载时,我可以得到它,但都是 (1,1,1,1,1,1,1)。

DownloadImageTask我有日志字符串namereviewImageLink. 结果很好。它显示不同的名称和网址。但

DownloadFullImageTaskonPostExcecute我也记录了 String ,name但它显示相同的名称(1,1,1,1,1,1,1)。

我怎样才能下载我所有的图像而不是相同的?

JSON

{"pictures": [
{
    "picture_url": "2de19d49ecc640e94b1a306944e161ef.png"
},
{
    "picture_url": "bee57cbb42c364592e7472b442c58868.jpg"
},
{
    "picture_url": "b718041660189162080ff86b09370ec8.jpg"
},
{
    "picture_url": "7022dec80bb604bcbba51d28ce8bc31c.jpg"
},
{
    "picture_url": "0be79b2cd531adf02487e8d54f736c0f.jpg"
},
{
    "picture_url": "2101134bcfc1356df1ff187a8f9510f8.jpg"
},
{
    "picture_url": "2de62e026b3378bbbdfe687cd43d5b0e.jpg"
}]}

AsyncTask 下载图片和json

public class DownloadImageTask extends AsyncTask<String, Void, Void> {

@Override
protected Void doInBackground(String... params) {

    placeList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject jsonO = jParser.getJSONUrl(url);

        // for " piture " Object in json
        pictures = jsonO.getJSONArray("pictures");
        for (int i = 0; i < pictures.length(); i++) {
            JSONObject jObj = pictures.getJSONObject(i);
            int cafe_id = jObj.getInt(TAG_CAFE_ID);
            String picture_url = jObj.getString(TAG_PICTURE_URL);

            // Table Picture
            Model_Insert model_Insert = new Model_Insert();
            model_Insert.setCafe_Id(cafe_id);
            model_Insert.setPitureUrl(picture_url);
            model_Insert = dataSource.createTablePicture(model_Insert);
            Log.i("pic", " Picture " + model_Insert.getPitureurl());
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_PICTURE_URL, picture_url);
            placeList.add(map);

            // Execute DownloadFullImageTask
            reviewImageLink = "http://xxx.xxx/xxx/place_pictures/"+picture_url;
            URL reviewImageURL;
            Log.i("url", reviewImageLink);
            String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1);
            try {
                reviewImageURL = new URL(reviewImageLink);
                if (!hasExternalStoragePublicPicture(name)) {
                    isImage = false;
                    new DownloadFullImageTask().execute(reviewImageURL);
                    Log.v("log_tag", "if");
                    isImage = true;
                    File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(),getResources()
                                    .getString(R.string.directory));
                    sdImageMainDirectory.mkdirs();
                    File file = new File(sdImageMainDirectory, name);
                    Log.v("log_tag", "Directory created");
                }

            } catch (MalformedURLException e) {
                Log.v(TAG, e.toString());
            }
        }

    } catch (JSONException e) {
        // TODO: handle exception
    }
    return null;
}

protected void onPostExecute(Void unused) {
     ShowAllContent(); // When Finish Show Content
}

}

AsyncTask 执行 url

public class DownloadFullImageTask extends AsyncTask<URL, Integer, Bitmap> {

protected Bitmap doInBackground(URL... paths) {
    URL url;
    try {
        url = paths[0];
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int length = connection.getContentLength();
        InputStream is = (InputStream) url.getContent();
        byte[] imageData = new byte[length];
        int buffersize = (int) Math.ceil(length / (double) 100);
        int downloaded = 0;
        int read;
        while (downloaded < length) {
            if (length < buffersize) {
                read = is.read(imageData, downloaded, length);
            } else if ((length - downloaded) <= buffersize) {
                read = is.read(imageData, downloaded, length - downloaded);
            } else {
                read = is.read(imageData, downloaded, buffersize);
            }
            downloaded += read;
            publishProgress((downloaded * 100) / length);
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
                length);
        if (bitmap != null) {
            Log.i(TAG, "Bitmap created");
        } else {
            Log.i(TAG, "Bitmap not created");
        }
        is.close();
        return bitmap;
    } catch (MalformedURLException e) {
        Log.e(TAG, "Malformed exception: " + e.toString());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.toString());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.toString());
    }
    return null;
}

protected void onPostExecute(Bitmap result) {
    String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1);
    if (result != null) {
        hasExternalStoragePublicPicture(name);
        saveToSDCard(result, name);
        isImage = true;

    } else {
        isImage = false;
    }
}
}
4

2 回答 2

1

onPostExecuteDownloadFullImageTask使用公共领域:reviewImageLink. 但是这个字段被你的其他任务改变了。要解决此问题,我建议您在 DownloadFullImageTask 中放置一个内部变量,您可以在其中保存来自 doInBackground 的路径 [0] 并改用它。

于 2013-03-07T06:58:48.250 回答
1

通过多线程加载、内存缓存或外部缓存获得更好的性能。我建议你看看这个图书馆

https://github.com/nostra13/Android-Universal-Image-Loader

它很强大!

于 2013-03-07T12:56:22.867 回答