내 listview의 각 요소에는 텍스트와 이미지가 포함되어 있습니다. JSON을 사용하여 url에서 텍스트를 가져 와서 listview에 표시 할 수 있습니다. 이미지의 URL을 가져올 수도 있지만 목록에 표시 할 수는 없습니다. 여기 내 코드입니다 :JSON을 사용하여 listview에서 URL의 이미지를로드 할 수 없습니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.newslist);
img_url = (TextView) findViewById(R.id.iamge_url);
contactList = new ArrayList<HashMap<String, Object>>();
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_POSTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
// JSONObject c = contacts.getJSONObject(i);
JSONObject posts = contacts.getJSONObject(i);
JSONArray attachment = posts.getJSONArray("attachments");
for (int j = 0; j< attachment.length(); j++){
JSONObject obj = attachment.getJSONObject(j);
JSONObject image = obj.getJSONObject("images");
JSONObject image_small = image.getJSONObject("tie-small");
String imgurl = image_small.getString("url");
Bitmap picture = getBitmapFromURL(imgurl);
HashMap<String, Object> contact = new HashMap<String, Object>();
contact.put("image_url", imgurl); //this image i want to load
contact.put("icon", picture); //this displays fine
contact.put(TAG_TITLE, title); // this displays fine
contactList.add(contact);
}
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, R.layout.third_row,
new String[] {"image_url",TAG_TITLE,"icon"},
new int[] {R.id.iamge_url,R.id.headline3,R.id.imageicon});
lv.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
이것은 URL에서 이미지에 geting 내 방법 getBitmapFromURL()이다 :
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
에 오류가 없습니다. 응용 프로그램도 충돌하지 않지만 목록보기에서는 이미지가 표시되지 않습니다. 내가 말한 logcat에 오류가 발생한다 :
unable to decode stream java.io.filenotfoundexception /android.graphics.bitmap (no such file or directory)
나는 url을 점검했다. 그것은 효과가있다. URL을 사용하여 브라우저에서 이미지를 볼 수 있습니다. 문제가 어디 있는지 알 수 없습니다.
이미지를 표시하고 싶다면 headImageViewHelper 또는 UniversalImageLoader와 같은 라이브러리를 사용하십시오. 여기에서는로드하려는 이미지의보기 및 URL을 전달해야합니다. (당신은 또한 이미지를 가져 오는 경우에 대비하여 3 번째 매개 변수로 기본 드로어 블을 제공 할 수 있습니다.) – Kaustuv
글쎄 .. 이클립스가 자바 힙 에러를 제공하기 시작하면서 라이브러리를 사용하는 것이 싫지만 .. 시도해 볼 것입니다. –