私のアプリケーションは、JSON からイメージ名を取得します。その名前を別のURLに連結して画像をダウンロードしました。すべての画像をダウンロードできますが、ダウンロードする画像はすべて同じです。たとえば、私の画像の名前は(1,2,3,4,5,6,7)ですが、ダウンロードするとすべてを取得できますが、すべてが(1,1,1,1,1,1,1)です。
DownloadImageTask
ログ文字列name
とreviewImageLink
. 結果は大丈夫でした。別の名前と URL が表示されます。しかし
DownloadFullImageTask
ononPostExcecute
私もログ 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;
}
}
}