2013-05-01 73 views
-1

我想從url中加載圖片,Android 2.2圖片加載但不是Android 4.0 ICS。它背後有什麼問題?還有什麼其他的方式可以做到這一點。來自Android的URL中的圖片加載問題

我的代碼是

public class Photos extends Activity { 

public static final String TAG_IMAGE_NAME = "image_name"; 
public static final String TAG_IMAGE_THUMB_NAME = "image_thumb_name"; 
public static String URL = "http://......./...../....../mainAPI.php"; 

ArrayList<HashMap<String, String>> photoList; 
String responseData = null; 
static GridView gridView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.photos); 
    gridView = (GridView)findViewById(R.id.gridView); 
    photoList = new ArrayList<HashMap<String,String>>(); 
    new AsyncData().execute(); 


} 

class AsyncData extends AsyncTask<String, Void, Void> { 
    ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute() { 
     pDialog = new ProgressDialog(Photos.this); 
     pDialog.setTitle("Loading...."); 
     pDialog.setMessage("Please wait..."); 
     pDialog.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(URL); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("rquest","{\"method\":\"photogallery\",\"body\":[{}]}")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 
      responseData = EntityUtils.toString(resEntity); 
      try { 
       JSONArray data = new JSONArray(responseData); 
       for (int i = 0; i < data.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        JSONObject c = data.getJSONObject(i); 
        String photoName = c.getString(TAG_IMAGE_NAME); 
        String imageThumbName = c.getString(TAG_IMAGE_THUMB_NAME); 
        map.put(TAG_IMAGE_NAME, photoName); 
        map.put(TAG_IMAGE_THUMB_NAME, imageThumbName); 
        photoList.add(map); 
       } 
      } catch (JSONException e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     ImageAdapter adapter = new ImageAdapter(Photos.this, R.layout.photo_row, photoList); 
     gridView.setAdapter(adapter); 
     if (pDialog != null && pDialog.isShowing()) { 
      pDialog.dismiss(); 
     } 
    } 
} 

ImageAdapter類。

public class ImageAdapter extends ArrayAdapter<HashMap<String, String>>{ 
Context context; 
ArrayList<HashMap<String, String>> myList; 
HashMap<String, String> myData; 
int layout; 
public ImageAdapter(Context context, int textViewResourceId, List<HashMap<String, String>> objects) { 
    super(context, textViewResourceId, objects); 
    // TODO Auto-generated constructor stub 
    this.context = context; 
    this.myList = (ArrayList<HashMap<String, String>>) objects; 
    this.layout = textViewResourceId; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View row = null; 
    ImageView image = null; 
    Bitmap bimage; 
    LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
    row = inflater.inflate(layout, parent, false); 
    myData = myList.get(position); 
    try{ 
     image = (ImageView)row.findViewById(R.id.imagePhoto); 
     String uri = ("" + myData.get(Photos.TAG_IMAGE_THUMB_NAME)).replace(" ", "%20"); 
     System.out.println(uri); 
     bimage = getBitmapFromURL(uri); 
     image.setImageBitmap(bimage); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
    return row; 
} 
public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    } 
} 

請給我一些解決方案。

+0

_它背後有什麼問題?_:你面臨什麼問題?它會崩潰嗎?如果是,發佈logcat。任何警告?發佈logcat。如果沒有這些,解釋發生了什麼。 _in Android 2.2圖像加載,但不在Android 4.0 ICS_很難說明。 – 2013-05-01 07:28:12

+0

它不會崩潰,但在Android 2.2中它顯示我想要的圖像,但在Android 4.0中,它什麼也沒有顯示,並且沒有發生錯誤。 – RBL 2013-05-01 07:32:54

回答

0

從Android 3.x開始,您無法在UI線程中執行網絡操作。在LogCat中,您應該看到錯誤NetworkOnMainThreadException。 http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

在你的情況下,它是從getView調用的getBitmapFromURL方法。你應該使用AsyncTask或一些線程

+0

我沒有收到任何錯誤,但它在Android 4.0上沒有顯示任何內容,它在Android 2.2中工作。 – RBL 2013-05-01 07:34:24

+0

我不知道爲什麼你看不到在Logcat中的錯誤,但你不能在UI ICS – httpdispatch 2013-05-01 07:40:40

+0

上的UI線程中執行網絡操作,你正在捕捉異常,這就是爲什麼應用程序沒有崩潰,但你應該看到錯誤消息日誌 – httpdispatch 2013-05-01 07:41:13