2013-02-25 78 views
0

目前,我正在開發一個Android應用程序,其中包含一個顯示Youtube視頻鏈接的列表視圖。應用程序從服務器獲取其數據爲JSON。現在我正試圖顯示來自此子域的這些視頻的縮略圖 - http://img.youtube.com/vi/從遠程URL下載圖像

但是圖像不顯示在列表視圖中。

下面是該項目的代碼:

1 - canticlesActivity.java

package com.shadatv.shada; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class canticlesActivity extends ListActivity { 

    TextView httpStuff; 
    HttpClient client; 
    JSONArray canticles; 
    String picpath = "http://img.youtube.com/vi/"; 

    File sdcard = Environment.getExternalStorageDirectory(); 

    File shadaRoot = new File(sdcard.getAbsolutePath() + "/shada_Folder"); 

    private static final String CA_NAME = "ca_name"; 
    private static final String CA_LINK = "ca_link"; 
    private static final String CA_IMG = "ca_img"; 
    private static final String URL = "http://dt-works.com/ags/shadatv/canticles/android_data"; 

    ArrayList<HashMap<String, String>> canticlesList; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.canticles); 

     httpStuff = (TextView) findViewById(R.id.textView1); 
     client = new DefaultHttpClient(); 
     new Read().execute(); 
    } 

    public JSONArray allCanticles() throws ClientProtocolException, IOException, JSONException { 
     StringBuilder url = new StringBuilder(URL); 

     HttpGet get = new HttpGet(url.toString()); 
     HttpResponse r = client.execute(get); 
     int status = r.getStatusLine().getStatusCode(); 

     if (status == 200) { 
      HttpEntity e = r.getEntity(); 
      String data = EntityUtils.toString(e); 
      JSONArray canticles = new JSONArray(data);   
      return canticles; 

     } else { 
      Toast.makeText(getBaseContext(), "error", Toast.LENGTH_SHORT).show(); 
      return null; 
     } 
    } 

    public void downloadImage(String fileURL) { 
     try { 
//   Toast.makeText(getBaseContext(), "baaad", Toast.LENGTH_SHORT).show(); 
      String finlpth = ""; 
      finlpth = picpath + fileURL + "/2.jpg"; 
      shadaRoot.mkdirs(); 

      URL u = new URL(finlpth); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      File DownloadedFile = new File(shadaRoot, fileURL + ".jpg"); 
      // if(!outfile.exists()) 
      FileOutputStream f = new FileOutputStream(DownloadedFile); 

      InputStream in = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = in.read(buffer)) > 0) { 
       f.write(buffer, 0, len1); 
      } 

      f.close(); 

     } catch (Exception e) { 
      Log.d("Downloader", e.getMessage()); 
     } 
    } 

    public class Read extends AsyncTask<String, String, String>{ 

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

      canticlesList = new ArrayList<HashMap<String, String>>(); 
      try { 
       canticles = allCanticles(); 
       for (int i = 0; i < canticles.length(); i++) { 
        JSONObject canticle = canticles.getJSONObject(i); 
        String ca_name = canticle.getString(CA_NAME); 
        String ca_link = canticle.getString(CA_LINK);     

        downloadImage(ca_link); 

        HashMap<String, String> map = new HashMap<String, String>();           
        map.put(CA_NAME, ca_name); 
        map.put(CA_LINK, ca_link); 
        map.put(CA_IMG, ca_link + ".jpg"); 
        canticlesList.add(map);              
       } 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      ListAdapter adapter = new SimpleAdapter(canticlesActivity.this, 
                canticlesList,R.layout.list_item, 
                new String[] {CA_NAME, CA_LINK, CA_IMG}, 
                new int[] {R.id.ca_name, R.id.ca_link, R.id.ca_img}); 
      setListAdapter(adapter); 

     }  
    } 
} 

2 - list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_selector" 
    android:orientation="horizontal" > 

    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity --> 

    <ImageView 
     android:id="@+id/ca_img" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:contentDescription="@string/desc"    
     /> 

    <TextView 
     android:id="@+id/ca_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

    <!-- Name Label --> 
    <TextView 
     android:id="@+id/ca_link" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dip" 
     android:paddingLeft="6dip" 
     android:textSize="17sp" 
     android:textStyle="bold" 
     android:visibility="gone" /> 

</LinearLayout> 
+0

這看起來像一個代碼轉儲給我。一次解決一個問題怎麼樣?從「沒有創建什麼目錄?」開始然後也許「文件不下載」。 – 2013-02-25 12:25:12

+0

我認爲這個問題在downloadImage方法中。看到我的答案從網址下載任何圖像http://stackoverflow.com/questions/13486758/android-bitmap-from-url-always-null/13487664#13487664 – 2013-02-25 12:29:43

+0

我認爲該方法本身不運行的主要問題因爲我評論了其中的所有代碼,然後把Toast放在一邊,它也不起作用 – 2013-02-25 12:32:35

回答

0

在youtube JSON數據,整個視頻信息將在「條目」JSON數組中。您將爲Hashmap中的所有條目創建相同的標籤名稱。如果爲所有條目賦予相同的標籤,則它將顯示最後一個條目圖像。請將TAG NAME改爲「map.put(CA_IMG + i,ca_link +」.jpg「);」 我希望這個解決方案對你有所幫助。

0

下載圖像,然後在ListView中顯示是不好的過程。因爲只要想想如果你的圖像> 100,那麼顯示它就需要更多的時間。

現在我建議的優化是:在ListView中實現延遲加載圖像的邏輯。

請參考下面的例子:

  1. Lazy List by Fedor
  2. Universal Image Loader
  3. ImageLoader by Novoda